获取"发送邮件失败"从Windows应用程序发送邮件时出现异常

时间:2014-03-11 12:46:43

标签: c#-4.0 smtp windows-applications

我正在尝试从我的C#Windows应用程序Localhost发送邮件。在发送邮件时,它会收到异常消息"发送邮件失败"。

smtpClient.Send(message);行。有什么问题?

           SmtpSection smtpSection = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as SmtpSection;

        StringBuilder bodyMessage = new StringBuilder();
        bodyMessage.Append("Error Importing the Asset XML File.");

        try
        {

            MailAddress to = new MailAddress("iramasani@gmail.com");
            MailAddress from = new MailAddress(smtpSection.From);
            MailMessage message = new MailMessage(from, to);
            message.Subject = "Error importing the Asset XML File.";
            message.Body = bodyMessage.ToString();                

            SmtpClient smtpClient = new SmtpClient(smtpSection.Network.Host, smtpSection.Network.Port);

            smtpClient.UseDefaultCredentials = true;
            smtpClient.Send(message);
        }
        catch (Exception ex)
        {
            LogError("Error Sending mail "+ ex.Message);                
        }
    }

app.config

<configuration>
     <system.net>
        <mailSettings>
            <smtp from="admin@alticore.com">                
                <network host="localhost" port="25" defaultCredentials="true" />
            </smtp>              
        </mailSettings>        
    </system.net>
</configuration>

0 个答案:

没有答案