sending email from gmail unity

Greetings, I am creating a project in unity it is an app to send mail to gmail works in unity but once created the .apk no longer works the mail is not sent, could you help me.

check device log for errors first,

Please use code tags rather than screenshots if you are simply sharing code.

Not sure what your end goal with this is, but if you put the password to your email account into a game that you distribute to the public, you should assume that someone at some point is going to learn the password from your game and start sending spam from your gmail account, and then Google will probably become upset with you.

If you’re only using this for friends/family or internally at your company or something like that, it might be OK. But if you’re planning to put this in the Play Store or something, you might want to reconsider.

How can I send an email through an app created in unity?

I believe the usual way to do this is to have the game contact some server that you control and then have the server send the mail (and put limitations on the quantity, content, and recipients of that email).

Alternately, you can require the user to provide their own email account for sending the emails (using their own username and password, not yours).

You generally need to assume that if your game can do something, then your customers will be able to reverse-engineer your game to figure out how to do that thing for themselves outside of your game. This is especially true when the thing in question is a communication that your game sends out from their computer, because then they probably don’t even need to look at your actual game; they can just monitor the signals their computer is sending out.

Anything you do that lets your game directly send emails through your account is likely to end up with hackers using your account to send spam.

i need an example

Take a look at mailgun examples for creating a service that sends emails for your game.

If you used the server suggestion from @Antistone you would google for how to do it from whatever server side language you choose to use. Maybe you would use PHPmailer for example. You’d interface with the server probably with UnityWebRequest. How to implement the server side part is pretty much outside the scope of a Unity forum.

As long as you can assume that your users have some sort of mail account on their device (which, if you’re targeting mobile at least, is 99% true), you can do something like this:

        string msg = "mailto:spamMe@mycompany.com";
        msg += "?subject=" + WWW.EscapeURL("Your Game Sucks!");
        msg += "&body=" + WWW.EscapeURL("Here's why I hate your game and want you to die...");
        Application.OpenURL(msg);

-Scott

3 Likes

This assumes their primary email is the one they are logged into their phone with. Some idiots like me have an account associated with their phone, but only do actual email through some other webmail service.

Can you help me with the complete code?

There’s really not much to it; if you want it in a function, it would look like this:

public static void SendMail(string toAddress, string subject, string body)
{
        string msg = "mailto:" + toAddress;
        msg += "?subject=" + WWW.EscapeURL(subject);
        msg += "&body=" + WWW.EscapeURL(body);
        Application.OpenURL(msg);
}
1 Like