Based on c # send Outlook e-mail (SMTP version only)

Outlook indicates that the first parameter: URL: https://support.office.com/zh-cn/article/Outlook-com-%E7%9A%84-POP%E3%80%81IMAP-%E5%92%8C- SMTP-% E8% AE% BE% E7% BD% AE-d088b986-291d-42b8-9564-9c414e2aa040

POP access is disabled by default. To enable POP access, see Enabling POP access in Outlook.com.

IMAP server name outlook.office365.com

IMAP port 993

IMAP TLS encryption method

POP server name outlook.office365.com

POP port 995

POP TLS encryption method

SMTP server name smtp.office365.com

SMTP port 587

SMTP STARTTLS encryption method


Nuget installation package: use as core project, it cites ReturnTrue.AspNetCore.Net.SmtpClient package (SmtpClient).

Official Code:

Yes, I am using SMTP client submission.

This is the c# code:

                var smtpClient = new SmtpClient()
                {
                    Host = "smtp.office365.com",
                    Port = 587,
                    UseDefaultCredentials = false,
                    EnableSsl = true
                };

                smtpClient.Credentials = new NetworkCredential("*** 发送邮箱账号 ***", " Password " ); 

                var Message = new new the MailMessage 
                { 
                    the From = new new the MailAddress ( " *** sending mail account *** " ), 
                    Sender = new new the MailAddress ( " *** receiving mail account *** " ), 
                    the Subject = " the Test mail " , 
                    IsBodyHtml = to false 
                }; 
                message.To.Add ( " *** receiving mail account *** ");

                message.Body = "This is a test mail. ";

                smtpClient.Send(message);

The code runs correctly, since the beginning of this code is not found, lead to code written in the following exception:

Message=The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [HK0P153CA0010.APCP153.PROD.OUTLOOK.COM]

Source: This is what I wrote the code before, in fact, is the same, but a start has been reported above error (reason is that before binding the account password to be set UseDefaultCredentials = false )

            string smtpServer = "smtp.office365.com";
            int smtpPort = 587;
            string mailFrom = "[email protected]";
            string passWord = "xxxxxx";
            string mailTo = "[email protected]";
            SmtpClient smtpClient = new SmtpClient(smtpServer, smtpPort);
            smtpClient.UseDefaultCredentials = false;//写到这里不报错
            = smtpClient.Credentials new new NetworkCredential (mailFrom, passWord); 
            smtpClient.EnableSsl = to true ;
             // smtpClient.UseDefaultCredentials = false; // write here will complain, must be written before the account password binding. 
            = MailAddressFrom the MailAddress new new the MailAddress (mailFrom); 
            the MailAddress MailAddressTo = new new the MailAddress (mailTo, " XX QQ mailbox " ); 
            the MailMessage MailMessage = new new the MailMessage (mailAddressFrom, MailAddressTo); 
            mailMessage.Subject = "With c # test Send Mail " ; 
            mailMessage.Body = " This is a test sent by the sender's mailbox outlook " ; 
            mailMessage.BodyEncoding = Encoding.UTF8; 
            smtpClient.Send (MailMessage);

 

 


 

Code is very short, only think of the order of object instantiation will produce such a result, later to pay attention.

Guess you like

Origin www.cnblogs.com/niubi007/p/11265452.html