我想用我的Gmail帐户发送电子邮件,而不是依靠我的主机发送电子邮件。这些邮件是发给我在节目中演出的乐队的个性化邮件。
有可能做到吗?
我想用我的Gmail帐户发送电子邮件,而不是依靠我的主机发送电子邮件。这些邮件是发给我在节目中演出的乐队的个性化邮件。
有可能做到吗?
当前回答
谷歌可能会阻止一些不使用现代安全标准的应用程序或设备的登录尝试。由于这些应用程序和设备更容易被侵入,屏蔽它们有助于让你的账户更安全。
不支持最新安全标准的应用程序包括:
iOS 6或以下版本的iPhone或iPad上的邮件应用程序 8.1版本之前Windows手机上的邮件应用程序 一些桌面邮件客户端,如Microsoft Outlook和Mozilla Thunderbird
因此,您必须在您的谷歌帐户中启用“较少安全登录”。
登录谷歌账号后,进入:
https://myaccount.google.com/lesssecureapps 或 https://www.google.com/settings/security/lesssecureapps
在c#中,你可以使用以下代码:
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress("email@gmail.com");
mail.To.Add("somebody@domain.com");
mail.Subject = "Hello World";
mail.Body = "<h1>Hello</h1>";
mail.IsBodyHtml = true;
mail.Attachments.Add(new Attachment("C:\\file.zip"));
using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
{
smtp.Credentials = new NetworkCredential("email@gmail.com", "password");
smtp.EnableSsl = true;
smtp.Send(mail);
}
}
其他回答
下面是一个从web.config发送邮件和获取凭证的方法:
public static string SendEmail(string To, string Subject, string Msg, bool bodyHtml = false, bool test = false, Stream AttachmentStream = null, string AttachmentType = null, string AttachmentFileName = null)
{
try
{
System.Net.Mail.MailMessage newMsg = new System.Net.Mail.MailMessage(System.Configuration.ConfigurationManager.AppSettings["mailCfg"], To, Subject, Msg);
newMsg.BodyEncoding = System.Text.Encoding.UTF8;
newMsg.HeadersEncoding = System.Text.Encoding.UTF8;
newMsg.SubjectEncoding = System.Text.Encoding.UTF8;
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
if (AttachmentStream != null && AttachmentType != null && AttachmentFileName != null)
{
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(AttachmentStream, AttachmentFileName);
System.Net.Mime.ContentDisposition disposition = attachment.ContentDisposition;
disposition.FileName = AttachmentFileName;
disposition.DispositionType = System.Net.Mime.DispositionTypeNames.Attachment;
newMsg.Attachments.Add(attachment);
}
if (test)
{
smtpClient.PickupDirectoryLocation = "C:\\TestEmail";
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory;
}
else
{
//smtpClient.EnableSsl = true;
}
newMsg.IsBodyHtml = bodyHtml;
smtpClient.Send(newMsg);
return SENT_OK;
}
catch (Exception ex)
{
return "Error: " + ex.Message
+ "<br/><br/>Inner Exception: "
+ ex.InnerException;
}
}
以及web.config中相应的部分:
<appSettings>
<add key="mailCfg" value="yourmail@example.com"/>
</appSettings>
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="yourmail@example.com">
<network defaultCredentials="false" host="mail.exapmple.com" userName="yourmail@example.com" password="your_password" port="25"/>
</smtp>
</mailSettings>
</system.net>
从另一个答案复制,上述方法工作,但gmail总是替换“从”和“回复”电子邮件与实际发送gmail帐户。然而,显然有一种方法:
http://karmic-development.blogspot.in/2013/10/send-email-from-aspnet-using-gmail-as.html
“3。在帐户选项卡,点击链接“添加另一个电子邮件地址你自己的”,然后验证它
或者可能是这个
更新3:读者德里克·贝内特说:“解决办法是进入你的gmail设置:账户,并“默认”一个gmail账户以外的账户。这将导致gmail用默认帐户的电子邮件地址重写From字段。”
这是不再支持,如果你正在尝试这样做现在。
https://support.google.com/accounts/answer/6010255?hl=en&visit_id=637960864118404117-800836189&p=less-secure-apps&rd=1#zippy=
来源:在ASP发送电子邮件。净c#
下面是一个使用c#发送邮件的示例工作代码,在下面的例子中,我使用谷歌的smtp服务器。
代码是相当不言自明的,替换电子邮件和密码与您的电子邮件和密码值。
public void SendEmail(string address, string subject, string message)
{
string email = "yrshaikh.mail@gmail.com";
string password = "put-your-GMAIL-password-here";
var loginInfo = new NetworkCredential(email, password);
var msg = new MailMessage();
var smtpClient = new SmtpClient("smtp.gmail.com", 587);
msg.From = new MailAddress(email);
msg.To.Add(new MailAddress(address));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = loginInfo;
smtpClient.Send(msg);
}
这是发送邮件的附件。简单而简短。
来源:http://coding-issues.blogspot.in/2012/11/sending-email-with-attachments-from-c.html
using System.Net;
using System.Net.Mail;
public void email_send()
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your mail@gmail.com");
mail.To.Add("to_mail@gmail.com");
mail.Subject = "Test Mail - 1";
mail.Body = "mail with attachment";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("c:/textfile.txt");
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("your mail@gmail.com", "your password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}