错误使用Gmail发送邮件

时间:2017-10-30 10:00:58

标签: c# email gmail

我创建了一个用于从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;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

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

相关问题