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?

love thakker
  • 1,136
  • 1
  • 16
  • 41
Hitesh Chandegara
  • 2,638
  • 6
  • 29
  • 49

4 Answers4

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);
Raghavendra C
  • 1,083
  • 1
  • 8
  • 23
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.

Joe McShea
  • 1,515
  • 9
  • 15
0

I know this question was asked some time ago. At that time using the SmtpClient was probably the way to go. Today, the recommended way to send email from an Office 365 account would be to use Microsoft Graph. Here are some resources on the subject.

Get started with Microsoft Graph

Outlook mail API overview

Automate creating, sending, and processing messages

Send mail

GraphServiceClient graphClient = new GraphServiceClient( authProvider );

var message = new Message { Subject = "Meet for lunch?", Body = new ItemBody { ContentType = BodyType.Text, Content = "The new cafeteria is open." }, ToRecipients = new List<Recipient>() { new Recipient { EmailAddress = new EmailAddress { Address = "fannyd@contoso.onmicrosoft.com" } } }, CcRecipients = new List<Recipient>() { new Recipient { EmailAddress = new EmailAddress { Address = "danas@contoso.onmicrosoft.com" } } } };

var saveToSentItems = false;

await graphClient.Me .SendMail(message,saveToSentItems) .Request() .PostAsync();

Rob Windsor
  • 12,648
  • 25
  • 39
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?

Damjan Tomic
  • 3,646
  • 3
  • 19
  • 18
  • 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