Created
June 13, 2012 15:48
-
-
Save groovyghoul/2924908 to your computer and use it in GitHub Desktop.
Sending email with attachment and credentials
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("[email protected]"); | |
myMessage.To.Add(new MailAddress("[email protected]")); | |
myMessage.Subject = "This is a test"; | |
myMessage.Body = "<html><body><br/><b>Sender Name:</b> Somebody<br/><br/><b>Email:</b> [email protected]<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("[email protected]", "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