无论计算机名称如何,都以c#发送电子邮件

时间:2013-06-13 17:51:12

标签: c# email smtp

我正在构建一个需要从客户端计算机发送电子邮件的应用

public static void sendMailLocal(string mailSubject, string userMail, string mailBody)
{
    try
    {
        string cliantMail = userMail;
        string subject = mailSubject;
        string body = string.Format("user email:{0}\n{1}",cliantMail,mailBody);
        string email;
        string password;
        string host;
        email = getMailAddrese();
        password = getPassword();
        host = getHostName();           
        var fromAddress = new MailAddress(email, "From Name",ncoding.Unicode);
        var toAddress = new MailAddress(email, "To Name");
        string fromPassword = password;
        SmtpClient smtp = new SmtpClient
        {
            Host = host,
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new tworkCredential(fromAddress.Address,fromPassword)
        };

        using (var message = new MailMessage(fromAddress, toAddress)
            {
                Subject = subject,
                Body = body
            })
        {
          smtp.Send(message);
        }
    }
    catch (Exception ex)
    {

    }
}

现在,此代码适用于某些计算机,而其他计算机则崩溃。经过一番调查后,我发现如果他们的名字是英文的,那就可以正常使用,但如果计算机名称是希伯来文,那就会崩溃。

所以有人知道我可以发送邮件的方式而不管计算机名称吗?

我得到的错误是system.net.mail.smtpexception失败发送邮件system.formatexception

我在smtp.send上得到它(消息);

0 个答案:

没有答案