簡體   English   中英

從.net通過gmail smtp發送郵件

[英]sending mail through gmail smtp from .net

我通過一些不同的帳戶在gmail服務器上設置了一些域。

我可以使用以下代碼從一個帳戶成功發送郵件(使用我登錄到cpanel的憑據),但是當我嘗試從其中一個用戶帳戶發送郵件(使用該用戶的憑據)時,沒有任何反應。

是否需要在gmail端進行設置才能啟用此設置?

這是有效的代碼,但僅來自我的域之一上的“父”帳戶:

 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);

當您嘗試通過Gmail服務器發送郵件時,必須使用Gmail憑據而不是域憑據。

例如,使用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