Code to send an e-mail with attachment from Unity using SMTP.


//Code in C#. Code is tested and working in Unity.

using UnityEngine;
using System;
using System.Net;
using System.Net.Mail;
using System.Collections;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

public class MailerScript : MonoBehaviour
string _sender = “”;
string _password = “”;
public MailerScript()
_sender = “/*sender email*/”;
_password = “/*sender password*/”;

void Start()

private void SendEmail()
//For File Attachment, more files can also be attached
Attachment att = new Attachment(@”/*url of the file*/”);
//tested only for files on local machine

//Hardcoded recipient email and subject and body of the mail
string recipient = “/*input recipient*/”;
string subject = “Test Mail”;
string message = “Hello World”;

SmtpClient client = new SmtpClient(““);
//SMTP server can be changed for gmail, yahoomail, etc., just google it up

client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(_sender, _password);
client.EnableSsl = true;

View original post 50 more words

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s