【问题标题】:Error Sending mail using Gmail使用 Gmail 发送邮件时出错
【发布时间】:2018-04-11 06:27:46
【问题描述】:

我创建了一个用于从 Gmail 发送邮件的 c# 代码,它从 ax 2012 接收邮件信息,如收据、CCMail、主题、正文、附件 它工作正常一个月然后问题不时出现 它给了我以下错误 发送邮件失败。和无法将数据写入传输连接:现有连接被远程主机强行关闭。 这是我的代码,我该如何修复它并使其稳定?

public class SendMail
{
    public String SendMessage(string recipientsEmail, string ccEmail, string emailSubject, string emailBody, String emailAttachments)
    {
        try
        {
            //For Sending to more than one recipients
            string to = recipientsEmail;
            string[] ToMultiple = to.Split(';');

            //For Sending to more than one CC
            string CC = ccEmail;
            string[] CCMultiple = CC.Split(';');

            //For Sending to more than one Attachment
            string Attachment = emailAttachments;
            string[] AttachmentMultiple = Attachment.Split('|');

            String ErrorMessage = "";

            //Connect to SMTP of Gmail
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

            //Email Details
            mail.From = new MailAddress("xxxxxxx");

            //Sending To Addresses
            foreach (var substring in ToMultiple)
            {
                bool Totest = IsValidEmail(substring);
                if (Totest == true)
                {
                    mail.To.Add(substring);
                }
                else
                {
                    ErrorMessage += "Invalid Email : " + substring + "\n";
                }
            }

            //CC Mail Details
            if (!string.IsNullOrEmpty(ccEmail))
            {
                foreach (var ccString in CCMultiple)
                {
                    bool CCtest = IsValidEmail(ccString);
                    if (CCtest == true)
                    {
                        mail.CC.Add(ccString);
                    }
                    else
                    {
                        ErrorMessage += "Invalid Email : " + ccString + "\n";
                    }
                }
            }
            //Attachment Mail Details
            if (emailAttachments != "")
            {
                foreach (var AttachmentString in AttachmentMultiple)
                {
                    if (File.Exists(AttachmentString))
                    {
                        Attachment attached = new Attachment(AttachmentString, MediaTypeNames.Application.Octet);
                        mail.Attachments.Add(attached);
                    }
                    else
                    {
                        ErrorMessage += "File : " + AttachmentString + " Not Exist. \n";
                    }
                }
            }
            //Email Body and Subject
            mail.Subject = emailSubject;

            if (string.IsNullOrEmpty(ErrorMessage))
            {
                mail.Body = emailBody;
            }
            else
            {
                mail.Body = emailBody + "\n" + ErrorMessage;
            }

            //Connect to Port and Credentials
            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("xxxxxxxx", "xxxxxxxx");
            SmtpServer.EnableSsl = true;

            //Send Mail
            SmtpServer.Send(mail);
            return ErrorMessage;
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }

    //Validate Email
    bool IsValidEmail(string email)
    {
        try
        {
            MailAddress m = new MailAddress(email);

            return true;
        }
        catch (FormatException)
        {
            return false;
        }
    }
}

【问题讨论】:

  • 哪一行抛出异常?
  • 您是否尝试过 465 端口? (需要 587 tls)
  • 您是否设置了 gmail 属性以允许外部连接?

标签: c# email gmail


【解决方案1】:

允许远程 IP 地址和外部连接对 gmail 的权限。
要进行身份验证,您需要允许来自您的 gmail 帐户中安全性较低的应用程序的访问。
Google G Suite 和 Gmail 使用不同的端口。 如果 587 不起作用,请尝试 467 端口。
如果您在网站上安装了安全套接字层 (SSL),那么这些可能会导致问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 1970-01-01
    • 2017-02-25
    • 1970-01-01
    • 2016-06-03
    • 2015-12-21
    相关资源
    最近更新 更多