使用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)

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

您需要允许客户端使用gmail帐户发送电子邮件。stackoverflow上的这个答案可以帮助您:如何通过Gmail 用C#发送电子邮件

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

最新更新