winforms没有使用app.config

时间:2018-02-16 08:32:43

标签: c# 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:发送邮件失败。 ---&GT; System.Net.WebException:无法连接到远程服务器&gt;&gt; ---&GT; System.Net.Sockets.SocketException:连接尝试失败,因为连接方没有正确响应&gt;&gt;经过一段时间或建立的连接失败,因为连接的主机无法响应74.125.24.108:467    在System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)    在System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure,Socket s4,Socket s6,Socket&amp; socket,IPAddress&amp; address,ConnectSocketState state,IAsyncResult asyncResult,Exception&amp; exception)    ---内部异常堆栈跟踪结束---    在System.Net.Mail.SmtpConnection.ConnectAndHandshakeAsyncResult.End(IAsyncResult结果)    在System.Net.Mail.SmtpTransport.EndGetConnection(IAsyncResult结果)    在System.Net.Mail.SmtpClient.ConnectCallback(IAsyncResult结果)    ---内部异常堆栈跟踪结束---

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

编辑:请注意,我最近添加了两行代码,因为我在发布时错过了它们。但它仍然无法运作

3 个答案:

答案 0 :(得分:0)

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

关于端口:我使用了端口587一次。

答案 1 :(得分:0)

试试这个:

注意 一定要使用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);
}

答案 2 :(得分:0)

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

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

并更改了<network/>中的app.config,如下所示

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

然后,它工作