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


Spartechus

//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()
{
SendEmail();
}

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-mail.outlook.com“);
//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;
client.Credentials…

View original post 50 more words

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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