Skip to content

Instantly share code, notes, and snippets.

@groovyghoul
Created June 13, 2012 15:48
Show Gist options
  • Save groovyghoul/2924908 to your computer and use it in GitHub Desktop.
Save groovyghoul/2924908 to your computer and use it in GitHub Desktop.
Sending email with attachment and credentials
using System;
using System.Net.Mail;
using System.Windows.Forms;
namespace TestingMail
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
CreateMessageWithAttachment();
}
public static void CreateMessageWithAttachment()
{
MailMessage myMessage = new MailMessage();
myMessage.From = new MailAddress("from@email.com");
myMessage.To.Add(new MailAddress("to@email.com"));
myMessage.Subject = "This is a test";
myMessage.Body = "<html><body><br/><b>Sender Name:</b>&nbsp;Somebody<br/><br/><b>Email:</b>&nbsp;to@email.com<br/><br/></body></html>";
myMessage.IsBodyHtml = true;
myMessage.Attachments.Add(new Attachment("readme.txt"));
SmtpClient mySmtp = new SmtpClient();
mySmtp.Host = "smtp.emailserver.com";
mySmtp.Credentials = new System.Net.NetworkCredential("from@email.com", "thepassword");
mySmtp.EnableSsl = true;
mySmtp.Send(myMessage);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment