Coding Smarter
Official Blog of Al Katawazi

Emailing with ASP.Net with Credentials

May 13, 2008 11:53 by Al Katawazi

There really isn't a lot on the net about this, and the material I did find was lacking. So here is my code block for sending emails using ASP.Net System.Mail class passing in credentials.

You will need the following using statements at the top, System.Web.Mail and this is for dotnet 2.0 or 3.5 frameworks.

MailMessage message = new MailMessage();
message.From = new MailAddress(@"Someone@url.com", "Someones Name");
message.To.Add(new MailAddress("SendingTo@url.com"));
message.Attachments.Add(new Attachment(@"C:\AttachmentLocation\"));
message.Subject = "Subject Line";
message.Body = "Contents of Email";
SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential("Username","Password");
client.Host = "smtp.mywebsite.com";
client.Port = 25;
client.Send(message);

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Related posts

Comments

May 13. 2008 18:29

Personally I like setting it in my web.config file so I don't have to do personalization settings in my web for something that is covered by the framework.

<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="noreply@ideapipe.com">
<network host="localhost" />
</smtp>
</mailSettings>
</system.net>

Nick Berardi

May 13. 2008 18:32

Thanks for the input nick, I agree with you but in this particular case I had to make a packaged product so it had to be database driven.

Al Katawazi

May 13. 2008 18:33

Well that is the nice thing about a package driven product, you can include a config file with it. Smile

Nick Berardi

Add comment


 

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

August 20. 2008 05:34