解決.net中使用gmail.com郵箱進行Smtp發送信件時失敗的問題

來源:互聯網
上載者:User
我經常使用免費的gmail.com郵箱,因為它容量較大,但我們在使用.net編程實現郵件發送時,常會出現我們意想不到的錯誤。最常見的就是:

(1)The operation has timed out.
(2)出現類似提示:
"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first ...."

 出現此類錯誤的主要原因是:
(1)POP的設定不對;
(2)SmtpClient的UseDefaultCredentials,Credentials,EnableSsl設定不正確所引起。

 首先,需要進入gmail.com郵箱,進行"轉寄和 POP/IMAP"的選項設定。如:

然後,在你的Smtp程式中設定以下幾項即可:
            // 建立 SmtpClient 以發送 Email
            SmtpClient client = new SmtpClient();

            MailMessage message = new MailMessage();

            // 設定發信人的EMAIL地址
            message.From = new MailAddress(fromAddress);

            // 設定收信人的EMAIL地址
            message.To.Add(toAddress);
            // 設定回複的EMAIL地址
            message.ReplyTo = new MailAddress(replyToAddress);

            // 設定抄送的EMAIL地址
            // message.CC.Add(ccAddress);
            // message.Bcc.Add(bccAddress);

            // 設定發信主題及內容
            message.Subject = msgSubject;
            message.Body = body;
            message.IsBodyHtml = isHtmlBody;
            // 設定SMTP host及連接埠
            client.Host = "smtp.gmail.com";
            client.Port = 25;
            client.UseDefaultCredentials = false;
            System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential(fromAddress, smtpPassword);
            client.Credentials = basicAuthenticationInfo;
            client.EnableSsl = true;
            client.Send(message);

祝程式發送給你帶來快樂享受!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.