6

How to send email through office 365 outlook account programatically?

I tried with below code to send a custom email but getting exception on first line,

Code

SmtpClient mailClient = new SmtpClient("smtp.office365.com");
mailClient.Port = 587;  
mailClient.EnableSsl = true;  
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("username@domain.com", "password");  
mailClient.Credentials = cred;  
MailMessage message = new MailMessage();  
message.From = new MailAddress("username@domain.com", "DisplayName");  
message.To.Add("username@domain.com");  
message.Subject = "Test subject";  
message.Body = "Test body";  
mailClient.Send(message);

Is there any way to send custom mail from office 365?

0

Can you give some more information about the Exception that you got? I've just tried your code and it worked for me (with my credentials of course).

Maybe you forgot to add the

using System.Net.Mail; 

statement?

  • Request for the permission of type 'System.Net.Mail.SmtpPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. not working – Rajesh Joshi Jun 16 '15 at 11:09
0

For SmtpClient Object may be you should not pass the "smtp.office365.com"

Bellow mentioned code works fine for me, please check and let me know

Using System.Net;
using System.Net.Mail;

string SPUsername = "YourMailAddress@outlook.com"
string ToMailAddress =  "ToMailAddress@Domain.com"
string Subject = "Subject of the mail"
string Htmlbody = "<html>mail body with or without html format</html>"

SmtpClient client = new SmtpClient();
String mailPassword = @"Enter Your Password Here";
SecureString secureMailpassword = new SecureString();
foreach (char c in mailPassword)
{
    secureMailpassword.AppendChar(c);
}
client.Port = 587;
client.Host = "outlook.office365.com";
client.EnableSsl = true;
client.Timeout = 100000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(SPUsername, secureMailpassword);
System.Net.Mail.MailMessage reportEmail = new System.Net.Mail.MailMessage(SPUsername, ToMailAddress, Subject, Htmlbody);
reportEmail.BodyEncoding = UTF8Encoding.UTF8;
reportEmail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
reportEmail.IsBodyHtml = true;
client.Send(reportEmail);
0

Where are you tying to execute this code from? i.e. local command line, web app running on a shared hosting provider, etc.

I'm asking because if you're trying to run this code in a shared hosting environment, they may not have given you sufficient privileges to specify the port number in SmtpClient. A quick way to test that would be to try changing the port to 25 (the default for SMTP). I believe this should work with office 365, but even if it doesn't you'd get a different error message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.