我想用我的Gmail帐户发送电子邮件,而不是依靠我的主机发送电子邮件。这些邮件是发给我在节目中演出的乐队的个性化邮件。
有可能做到吗?
我想用我的Gmail帐户发送电子邮件,而不是依靠我的主机发送电子邮件。这些邮件是发给我在节目中演出的乐队的个性化邮件。
有可能做到吗?
当前回答
这是不再支持,如果你正在尝试这样做现在。
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);
}
一定要使用System.Net。邮件,而不是已弃用的System.Web.Mail。使用System.Web.Mail进行SSL是一堆拙劣的扩展。
using System.Net;
using System.Net.Mail;
var fromAddress = new MailAddress("from@gmail.com", "From Name");
var toAddress = new MailAddress("to@example.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
此外,转到谷歌帐户>安全页面,并查看登录谷歌> 2步验证设置。
如果启用了,那么您必须生成一个密码,允许. net绕过2步验证。点击“Signing in To谷歌> App passwords”,选择App = Mail, device = Windows Computer,最后生成密码。在fromPassword常量中使用生成的密码,而不是标准的Gmail密码。 如果它被禁用,那么你必须打开Less secure app access,这是不推荐的!所以最好启用两步验证。
我希望这段代码可以正常工作。你可以试试。
// Include this.
using System.Net.Mail;
string fromAddress = "xyz@gmail.com";
string mailPassword = "*****"; // Mail id password from where mail will be sent.
string messageBody = "Write the body of the message here.";
// Create smtp connection.
SmtpClient client = new SmtpClient();
client.Port = 587;//outgoing port for the mail.
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(fromAddress, mailPassword);
// Fill the mail form.
var send_mail = new MailMessage();
send_mail.IsBodyHtml = true;
//address from where mail will be sent.
send_mail.From = new MailAddress("from@gmail.com");
//address to which mail will be sent.
send_mail.To.Add(new MailAddress("to@example.com");
//subject of the mail.
send_mail.Subject = "put any subject here";
send_mail.Body = messageBody;
client.Send(send_mail);
试试这个
public static bool Send(string receiverEmail, string ReceiverName, string subject, string body)
{
MailMessage mailMessage = new MailMessage();
MailAddress mailAddress = new MailAddress("abc@gmail.com", "Sender Name"); // abc@gmail.com = input Sender Email Address
mailMessage.From = mailAddress;
mailAddress = new MailAddress(receiverEmail, ReceiverName);
mailMessage.To.Add(mailAddress);
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = true;
SmtpClient mailSender = new SmtpClient("smtp.gmail.com", 587)
{
EnableSsl = true,
UseDefaultCredentials = false,
DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential("abc@gmail.com", "pass") // abc@gmail.com = input sender email address
//pass = sender email password
};
try
{
mailSender.Send(mailMessage);
return true;
}
catch (SmtpFailedRecipientException ex)
{
// Write the exception to a Log file.
}
catch (SmtpException ex)
{
// Write the exception to a Log file.
}
finally
{
mailSender = null;
mailMessage.Dispose();
}
return false;
}
在Gmail / Outlook.com邮箱更改发件人:
为了防止欺骗- Gmail/Outlook.com不会让你从任意用户帐户名发送。
如果发件人数量有限,则可以按照以下说明将From字段设置为此地址:从不同地址发送邮件
如果你想从一个任意的电子邮件地址发送(比如用户在网站上输入他们的电子邮件,而你不希望他们直接给你发送电子邮件),你能做的最好的是:
msg.ReplyToList.Add(new System.Net.Mail.MailAddress(email, friendlyName));
这可以让你在电子邮件账户中点击“回复”,在反馈页面上回复你乐队的粉丝,但他们不会收到你的实际电子邮件,这可能会导致大量垃圾邮件。
如果你在一个受控制的环境中,这工作得很好,但请注意,我看到一些电子邮件客户端发送到from地址,即使回复指定(我不知道是哪个)。