首页 > 解决方案 > 使用其他邮件服务器时发送电子邮件时出错,期望 gmail

问题描述

我正在尝试从邮件地址(hassan@creativesolutionzone.com)发送电子邮件,但它没有发送未处理的异常:

System.net.mail.smtp 异常:邮箱不可用。服务器响应被拒绝访问 - HELO 名称无效(请参阅 RFC2821 4.1.1.1)

这是代码..

private void sendMail() 
{
       login = new NetworkCredential(txtUsername.Text, txtPassword.Text);
       client = new SmtpClient(txtSMTP.Text);
       client.Port = Convert.ToInt32(txtport.Text);
       client.EnableSsl = chkSSL.Checked;
       client.Credentials = login;
       msg = new MailMessage { From = new MailAddress(txtUsername.Text + txtSMTP.Text.Replace("mail.", "@"), "Creative Solution Zone", Encoding.UTF8) };
       msg.To.Add(txtTO.Text);
       if (!string.IsNullOrEmpty(txtCC.Text))
           msg.To.Add(new MailAddress(txtCC.Text));
       msg.Subject = txtSubject.Text;
       msg.Body = txtMessege.Text;
       msg.BodyEncoding = Encoding.UTF8;
       msg.IsBodyHtml = true;
       msg.Priority = MailPriority.Normal;
       msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
       client.SendCompleted += new SendCompletedEventHandler(SendCompleteCallBack);
       string userState = "Sending...";
       client.SendAsync(msg, userState);
}


private static void SendCompleteCallBack(object sender, AsyncCompletedEventArgs e)
{
    if (e.Cancelled)
        MessageBox.Show(string.Format("{0} Send Cancelled ", e.UserState), "Messgae", MessageBoxButtons.OK, MessageBoxIcon.Information);
    if (e.Error != null)
        MessageBox.Show(string.Format("{0}", e.Error), "Messgae", MessageBoxButtons.OK, MessageBoxIcon.Information);
    else
        MessageBox.Show("Your Message Has Been Successfully Sent", "Messgae", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

谁能帮我解决这个问题?

注意:当我尝试从我的 Gmail 帐户发送邮件时,代码运行良好,但当我更改邮件时它无法正常工作

标签: c#emailexception-handlingsmtpgmail

解决方案


推荐阅读