Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Aspose.Email - Create and Send Emails
// Create a new instance of MailMessage class
MailMessage message = new MailMessage();
// Set subject of the message, Html body and sender information
message.Subject = "New message created by Aspose.Email for .NET";
message.HtmlBody = "<b>This line is in bold.</b> <br/> <br/>" + "<font color=blue>This line is in blue color</font>";
message.From = new MailAddress("from@domain.com", "Sender Name", false);
// Add To recipients and CC recipients
message.To.Add(new MailAddress("to1@domain.com", "Recipient 1", false));
message.CC.Add(new MailAddress("cc1@domain.com", "Recipient 3", false));
// Save message in EML/EMLX/MSG/MHTML format
message.Save("EmailMessage.eml", SaveOptions.DefaultEml);
// Create a new instance of MailMessage class
MailMessage message = new MailMessage();
// Set subject of the message, Html body, sender, and receiver information
message.Subject = "New message created by Aspose.Email for .NET";
message.HtmlBody = "<b>This line is in bold.</b> <br/> <br/>" + "<font color=blue>This line is in blue color</font>";
message.From = new MailAddress("from@domain.com", "Sender Name", false);
message.To.Add(new MailAddress("to1@domain.com", "Recipient 1", false));
// Specify encoding
message.BodyEncoding = Encoding.ASCII;
// Save message in EML/EMLX/MSG/MHTML format
message.Save("EmailMessage.msg", SaveOptions.DefaultMsgUnicode);
// Create a new instance of MailMessage class
MailMessage message = new MailMessage();
// Set subject of the message, body and sender information
message.Subject = "New message created by Aspose.Email for .NET";
message.Body = "This is the body of the email.";
message.From = new MailAddress("from@domain.com", "Sender Name", false);
// Add To recipients and CC recipients
message.To.Add(new MailAddress("to1@domain.com", "Recipient 1", false));
message.CC.Add(new MailAddress("cc1@domain.com", "Recipient 3", false));
// Add attachments
message.Attachments.Add(new Attachment("word.docx"));
// Save message in EML, EMLX, MSG and MHTML formats
message.Save("EmailMessage.eml", SaveOptions.DefaultEml);
message.Save("EmailMessage.emlx", SaveOptions.CreateSaveOptions(MailMessageSaveType.EmlxFormat));
message.Save("EmailMessage.msg", SaveOptions.DefaultMsgUnicode);
message.Save("EmailMessage.mhtml", SaveOptions.DefaultMhtml);
// Create SmtpClient and specify server, port, user name and password
SmtpClient client = new SmtpClient("mail.server.com", 25, "Username", "Password");
// Create instances of MailMessage class and specify To, From, Subject and Message
MailMessage message1 = new MailMessage("msg1@from.com", "msg1@to.com", "Subject1", "message1, how are you?");
MailMessage message2 = new MailMessage("msg1@from.com", "msg2@to.com", "Subject2", "message2, how are you?");
MailMessage message3 = new MailMessage("msg1@from.com", "msg3@to.com", "Subject3", "message3, how are you?");
// Create an instance of MailMessageCollection and add email messages to it
MailMessageCollection manyMsg = new MailMessageCollection();
manyMsg.Add(message1);
manyMsg.Add(message2);
manyMsg.Add(message3);
try
{
// Send messages
client.Send(manyMsg);
}
catch (Exception ex)
{
Trace.WriteLine(ex.ToString());
}
// Create MailMessage instance. You can create a new message or load a already created message file (eml, msg, etc.)
MailMessage msg = MailMessage.Load("EmailMessage.msg");
// Create an instance of SmtpClient class
SmtpClient client = new SmtpClient();
// Specify your mailing Host, Username, Password, Port # and Security option
client.Host = "mail.server.com";
client.Username = "username";
client.Password = "password";
client.Port = 587;
client.SecurityOptions = SecurityOptions.SSLExplicit;
object state = new object();
IAsyncResult ar = client.BeginSend(msg, Callback, state);
Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.");
string answer = Console.ReadLine();
// If the user canceled the send, and mail hasn't been sent yet,
if (answer != null && answer.StartsWith("c"))
{
client.CancelAsyncOperation(ar);
}
msg.Dispose();
// --- Callback ---
static AsyncCallback Callback = delegate (IAsyncResult ar)
{
var task = ar as IAsyncResultExt;
if (task != null && task.IsCanceled)
{
Console.WriteLine("Send canceled.");
}
if (task != null && task.ErrorInfo != null)
{
Console.WriteLine("{0}", task.ErrorInfo);
}
else
{
Console.WriteLine("Message Sent.");
}
};
// Create MailMessage instance. You can create a new message or load a already created message file (eml, msg, etc.)
MailMessage msg = MailMessage.Load("EmailMessage.msg");
// Create an instance of SmtpClient class
SmtpClient client = new SmtpClient();
// Specify your mailing Host, Username, Password, Port # and Security option
client.Host = "mail.server.com";
client.Username = "username";
client.Password = "password";
client.Port = 587;
client.SecurityOptions = SecurityOptions.SSLExplicit;
try
{
// Send this email
client.Send(msg);
}
catch (Exception ex)
{
Trace.WriteLine(ex.ToString());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.