使用gmail帐户发送电子邮件一段时间后停止工作

时间:2016-07-25 11:22:49

标签: c# asp.net email gmail

我们有一个iphone应用程序的cpanel,我们向注册用户发送电子邮件。为了早些时候发送电子邮件,我们有自己的服务器,但是从gmail购买了电子邮件,以便他可以使用app@mywebsite.com登录gmail。

为了工作,我们将代码更改为gmail设置,一切正常。突然,今天客户打电话给我们说电子邮件无效。

明白为什么gmail会停止发送电子邮件?我看到它在开始时有效,但有一段时间后谷歌停止发送电子邮件。

以下是使用的代码。

public static bool SendMail(string From, string To, string SMTPServer, string UserName, string Password, string Subject, string Body, string AttachmentFilePath = null)
{
    if (!string.IsNullOrEmpty(From) && !string.IsNullOrEmpty(To) && !string.IsNullOrEmpty(SMTPServer) && !string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(Password))
    {
        try
        {
            MailMessage MailMessageObj = new MailMessage();
            MailMessageObj.From = From;
            MailMessageObj.To = To;
            MailMessageObj.Subject = Subject;
            MailMessageObj.Body = Body;
            MailMessageObj.BodyFormat = MailFormat.Html;
            MailMessageObj.Priority = MailPriority.High;
            MailMessageObj.BodyEncoding = System.Text.Encoding.UTF8;
            if (!string.IsNullOrEmpty(AttachmentFilePath))
                MailMessageObj.Attachments.Add(new System.Web.Mail.MailAttachment(HttpContext.Current.Server.MapPath(AttachmentFilePath)));
            MailMessageObj.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
            MailMessageObj.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = UserName;
            MailMessageObj.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = Password;

            System.Web.Mail.SmtpMail.SmtpServer = SMTPServer;
            System.Web.Mail.SmtpMail.Send(MailMessageObj);
            return true;
        }
        catch (Exception ex)
        {
            LogException(ex);
            return false;
        }
    }
    return false;
}

以下是我的例外

Exception Message :   
The server rejected one or more recipient addresses. The server response was: 550 5.1.1  https://support.google.com/mail/answer/6596 c202si11359510oib.218 - gsmtp


Exception InnerException :   
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException: The server rejected one or more recipient addresses. The server response was: 550 5.1.1  https://support.google.com/mail/answer/6596 c202si11359510oib.218 - gsmtp

   --- End of inner exception stack trace ---
   at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at System.Web.Mail.SmtpMail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args)

Exception StackTrace :   
   at System.Web.Mail.SmtpMail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args)
   at System.Web.Mail.SmtpMail.CdoSysHelper.Send(MailMessage message)
   at System.Web.Mail.SmtpMail.Send(MailMessage message)
   at Utilities.SendMail(String From, String To, String SMTPServer, String UserName, String Password, String Subject, String Body, String AttachmentFilePath)

2 个答案:

答案 0 :(得分:0)

我想我不久前也遇到过这个问题。它与gmail的隐私设置有关。

您需要允许客户使用Gmail帐户发送电子邮件。 stackoverflow上的这个答案可以帮助您: How to send an e-mail with C# through Gmail

答案 1 :(得分:0)

我不知道我的建议是否对您有所帮助,但当我在Android上发送gmail消息时,问题是用户名或密码错误。 第一次尝试登录Gmail时,如果您的密码或用户名错误,则在重新启动应用程序之前无法再次尝试,否则您将无法登录,如果重新启动应用程序,则可以尝试登录。认为在应用程序中使用gmail有一个条件,包括尝试登录。祝你好运