简体   繁体   English

从.net通过gmail smtp发送邮件

[英]sending mail through gmail smtp from .net

I have a few domains set up on the gmail servers through a few different accounts. 我通过一些不同的帐户在gmail服务器上设置了一些域。

I can successfully send mail using the following code from one of the accounts (using the credentials I use to log into the cpanel), but when I try and send mail from one of the user accounts (using that user's credentials), nothing goes out. 我可以使用以下代码从一个帐户成功发送邮件(使用我登录到cpanel的凭据),但是当我尝试从其中一个用户帐户发送邮件(使用该用户的凭据)时,没有任何反应。

Is there a setting I need to set on the gmail side to enable this? 是否需要在gmail端进行设置才能启用此设置?

here is the code that works but from only the "parent" account on one of my domains: 这是有效的代码,但仅来自我的域之一上的“父”帐户:

 SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
        client.EnableSsl = true;
        client.Credentials = new NetworkCredential("someaddress@somedomain.com", "somepassword");
        MailMessage msg = new MailMessage();
        msg.To.Add(new MailAddress("someuser@someotherdomain.com"));
        msg.Subject = "Inquiry from blah blah blah";
        msg.IsBodyHtml = true;
        msg.Body = "blah blah blah";
        msg.From = new MailAddress("someaddress@somedomain.com");
        client.Send(msg);

When you are trying to send mail frim gmail servers, you must use gmail credentials not your domain credentials. 当您尝试通过Gmail服务器发送邮件时,必须使用Gmail凭据而不是域凭据。

for example use someaddress@gmail.com & your gmail password. 例如,使用someaddress@gmail.com和您的Gmail密码。

MailMessage mail = new MailMessage();
        mail.To.Add("recipient@yahoo.com");

        mail.From = new MailAddress("sommeemail@gmail.com", "sender name");
        mail.Subject = subject;
        mail.Body = body;
        mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = new System.Net.NetworkCredential("sommeemail@gmail.com","password");
        smtp.EnableSsl = true;    
        smtp.Send(mail);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM