winforms 不使用来自 app.config 的凭据向 gmail 发送邮件

winforms not sending mail to gmail with credentials from app.config

我正在尝试创建一个代码原型,通过引用 SMTPClient - msdn 和 winforms

可以将电子邮件发送到我的 gmail
protected void sendMail()
    {



        MailAddress from = new MailAddress("xyz@gmail.com", "Sender", System.Text.Encoding.UTF8);
        MailAddress to = new MailAddress("xyz@gmail.com");
        MailMessage message = new MailMessage(from, to);


        message.Body = "This is a test e-mail message sent by an application. ";
        message.BodyEncoding = System.Text.Encoding.UTF8;
        message.Subject = "test message 1";
        message.SubjectEncoding = System.Text.Encoding.UTF8;

        SmtpClient client = new SmtpClient();
        client.EnableSsl = true;
        client.UseDefaultCredentials = false;
        client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
        string userState = "test message1";
        client.SendAsync(message, userState);
        message.Dispose();
    }

private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
    {
        String token = (string)e.UserState;

        if (e.Cancelled)
        {
            MessageBox.Show("send cancelled", "Mail status");
        }
        if (e.Error != null)
        {
            MessageBox.Show(e.Error.ToString(), "Mail status");
        }
        else
        {
            MessageBox.Show("Message sent", "Mail status");
        }
        mailSent = true;
    }

app.config 看起来像这样:

<configuration>
<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.net>
    <mailSettings>
        <smtp from="xyz@gmail.com">
            <network host="smtp.gmail.com" userName="xyz@gmail.com" password="app_specific_password" port="467" defaultCredentials="false"/>
        </smtp>
    </mailSettings>
</system.net>
</configuration>

但是当我 运行 应用程序时,它似乎没有从配置文件中获取凭据。

收到的例外说明:

System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server >> ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond >> after a period of time or established connection failed because connected host has failed to respond 74.125.24.108:467 at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) --- End of inner exception stack trace --- at System.Net.Mail.SmtpConnection.ConnectAndHandshakeAsyncResult.End(IAsyncResult result) at System.Net.Mail.SmtpTransport.EndGetConnection(IAsyncResult result) at System.Net.Mail.SmtpClient.ConnectCallback(IAsyncResult result) --- End of inner exception stack trace ---

P.S.: 我已经搜索并尝试直接硬编码到实际有效的代码中,但我非常具体地使用 app.config 而不是那些方法

编辑:请记下,我最近添加了两行代码,因为我在发布时错过了它们。但是还是不行

您引用了 SMTPClient - msdn 但在此示例函数中有 args[]。您如何为您的函数提供登录名和密码?

关于端口:我用过一次587端口

试试这个:

备注 请务必使用 System.Net.Mail,而不是已弃用的 System.Web.Mail。用 System.Web.Mail 做 SSL 是一堆骇人听闻的扩展。

端口是587还是465,看看你需要哪个。

using System.Net;
using System.Net.Mail;

var fromAddress = new MailAddress("from@gmail.com", "From Name");
var toAddress = new MailAddress("to@example.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";

var smtp = new SmtpClient
{
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
    Subject = subject,
    Body = body
})
{
    smtp.Send(message);
}

谢谢你们试图帮助我。我通过从代码中删除这些行来解决它:

    client.EnableSsl = true;
    client.UseDefaultCredentials = false;

并在 app.config 中更改 <network/> 如下

<network host="smtp.gmail.com" userName="xyz@gmail.com" password="app_specific_password" port="587" enableSsl="true"/>

然后,成功了