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("[email protected]", "password");  
mailClient.Credentials = cred;  
MailMessage message = new MailMessage();  
message.From = new MailAddress("[email protected]", "DisplayName");  
message.To.Add("[email protected]");  
message.Subject = "Test subject";  
message.Body = "Test body";  
mailClient.Send(message);

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

4 Answers 4

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?

1
  • Request for the permission of type 'System.Net.Mail.SmtpPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. not working Jun 16, 2015 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 = "[email protected]"
string ToMailAddress =  "[email protected]"
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.

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 = "[email protected]"
            }
        }
    },
    CcRecipients = new List<Recipient>()
    {
        new Recipient
        {
            EmailAddress = new EmailAddress
            {
                Address = "[email protected]"
            }
        }
    }
};

var saveToSentItems = false;

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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