我想从任何电子邮件地址向Asp.net C#中的特定电子邮件发送电子邮件。 例如:mail@example.com是收件人姓名,用户希望从yahoo或gmail或hotmail发送电子邮件。帮助将非常感激。
public bool SendMail(string message, string usermail, string userpassword)
{
string[] emailadd = usermail.Split('@');
string username = emailadd[0];
string sufix = emailadd[1];
var emailClient = new SmtpClient();
var smtpUserInfo = new System.Net.NetworkCredential();
//For Gmail Email
if (sufix == "gmail.com")
emailClient.Host = "smtp.gmail.com";
// For yahoo Email
if (sufix == "yahoo.com")
emailClient.Host = "smtp.mail.yahoo.com";
//For Hotmail Email
if (sufix == "hotmail.com")
emailClient.Host = "smtp.live.com";
emailClient.Port = 587;
emailClient.EnableSsl = true;
smtpUserInfo.UserName = usermail;
smtpUserInfo.Password = userpassword;
emailClient.UseDefaultCredentials = false;
emailClient.Credentials = smtpUserInfo;
const string subject = "Subect Here";
var msg = new MailMessage(usermail, "info@abc.net", subject, message) { IsBodyHtml = true };
emailClient.Send(msg);
return true;
}
答案 0 :(得分:2)
using System.Net;
using System.Net.Mail;
string smtpAddress = "smtp.mail.yahoo.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "email@yahoo.com";
string password = "abcdefg";
string emailTo = "someone@domain.com";
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
// Can set to false, if you are sending pure text.
mail.Attachments.Add(new Attachment("C:\\SomeFile.txt"));
mail.Attachments.Add(new Attachment("C:\\SomeZip.zip"));
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
提到From
请在接收地址而不是
info@abc.com
上输入正确的电子邮件地址。还请从上面的示例代码中了解一下。
答案 1 :(得分:0)
您的Google电子邮件ID是否通过您的#发送邮件。通常,我们大多数没有邮件主机的人都可以使用这个简单的代码制作一个程序,可以将邮件发送到任何SMTP Web主机。我做了一个,我想与大家分享。请评论如何使其更具吸引力。
SmtpClient SmtpServer = new SmtpClient();
SmtpServer.Credentials = new System.Net.NetworkCredential("xyz@gmail.com", "password");
SmtpServer.Port = 587;
SmtpServer.Host = "smtp.gmail.com";
SmtpServer.EnableSsl = true;
mail = new MailMessage();
String[] addr = TextBox1.Text.Split(',');
try
{
mail.From = new MailAddress("youremail@gmail.com","Your Name", System.Text.Encoding.UTF8);
Byte i;
for( i = 0;i< addr.Length; i++)
mail.To.Add(addr[i]);
mail.Subject = TextBox3.Text;
mail.Body = TextBox4.Text;
if(ListBox1.Items.Count != 0)
{
for(i = 0;i<ListBox1.Items.Count;i++)
mail.Attachments.Add(new Attachment(ListBox1.Items[i].ToString()));
}
SmtpServer.Send(mail);
}
catch (Exception ex)
{
MessageBox.Show("Error" +ex.ToString());
}