本文介绍了为什么我使用Google'smtp'无法发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下程序通过使用"smtp.gmail.com:587"发送电子邮件.

I have the following program to send an email by using "smtp.gmail.com:587"

namespace TestMailServer
{
    class Program
    {
        static void Main(string[] args)
        {
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            mail.From = new MailAddress("myTest@gmail.com");
            mail.To.Add("myTest2@gmail.com");
            mail.Subject = "Test Mail";
            mail.Body = "This is for testing SMTP mail";

            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("myTest@gmail.com", "myPassword");
            SmtpServer.EnableSsl = true;
            SmtpServer.Send(mail);
            Console.WriteLine("Send out");

        }
    }
}

myTest @ gmail.com,myTest2 @ gmail.com确实存在,myTest @ gmail.com的密码为myPassword.为什么出现以下错误:

myTest@gmail.com, myTest2@gmail.com are really existing and myTest@gmail.com's password is myPassword. Why I got the following error:

推荐答案

我不确定是什么引起了您的问题.这是我用来通过gmail帐户成功发送电子邮件的一些代码:

I'm not sure what is causing your problem. Here is some code I have been using to successfully send email through a gmail account:

const string from = "...";
var fromAddr = new MailAddress(from, "Bug Tracker");
var toAddr = new MailAddress("...@...", "...");
var client = new SmtpClient {
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Timeout = 30 * 1000,
    Credentials = new NetworkCredential(fromAddr.Address, "...")
};
using (var msg = new MailMessage(fromAddr, toAddr)) {
    msg.Subject = "...";
    msg.Body = string.Format("username: {0}\nversion: {1}\n\n{2}", Environment.UserName, Assembly.GetExecutingAssembly().GetName().Version.ToString(3), cbtext);
    client.Send(msg);
}

这篇关于为什么我使用Google'smtp'无法发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 02:02