从asp.net发送电子邮件 [英] Sending email from asp.net
问题描述
你好朋友..
我正在asp.net中创建一个页面来发送邮件,其中凭据("userid"和"password")和发件人"应该不同.
例如:
如果我使用凭据发送邮件,
用户ID:abc@gmail.com
密码:123456
然后
收件人收到的邮件应显示为
来自:xyz@gmail.com
收件人:收件人@ gmail.com
主题:Hi
消息:亲爱的..
请注意发件人:"
请帮忙..
这是我正在使用的代码...
Hello friends..
I am creating a page in asp.net to send mails, where Credentials (''userid''and ''password'') and From are should be different.
for example:
if I am sending mail using creadentials,
UserID: abc@gmail.com
password:123456
then
mail received by recipient should appear as
From : xyz@gmail.com
To : recipient@gmail.com
Subjet: Hi
Message: Hello Dear..
Please notice the ''From : ''
Please help..
Here is the code that I am using...
System.Net.NetworkCredential _Credential = new System.Net.NetworkCredential(UserID, Password);
System.Net.Mail.MailMessage _MailMessage = new MailMessage();
_MailMessage.To.Add(txt_email.Text);
_MailMessage.Subject = subject;
_MailMessage.From = new System.Net.Mail.MailAddress(<big>From</big>);
_MailMessage.Body = message;
_MailMessage.IsBodyHtml = true;
System.Net.Mail.SmtpClient _SmtpClient = new System.Net.Mail.SmtpClient(smtp);
_SmtpClient.UseDefaultCredentials = true;
_SmtpClient.EnableSsl = true;
_SmtpClient.Credentials = _Credential;
_SmtpClient.Port = Convert.ToInt32(port);
_SmtpClient.Send(_MailMessage);
lbl_Err.Text = "Mail Sent Successfully!";
lbl_Err.ForeColor = System.Drawing.Color.Green;
lbl_Err.Visible = true;
推荐答案
设置发件人"地址时,会有一个替代项允许您使用:
When you set the From address, there is an override which allows this:
mail.From = new MailAddress(fromAddress, fromDisplay, Encoding.UTF8);
如果您在这里看看,这里有个技巧为整个过程提供了一种通用方法: ^ ]
If you have a look here there is a Tip which provides a generic method for the whole process: Sending an Email in C# with or without attachments: generic routine.[^]
默认情况下,电子邮件发送时带有 System.Web.Mail 的格式为纯文本.若要将格式设置为HTML,请将 MailMessage.BodyFormat 属性设置为 MailFormat.Html .
例如:
By default, email sent with System.Web.Mail is formatted as plain text. To format as Html, set the MailMessage.BodyFormat property to MailFormat.Html.
ex:
mail.BodyFormat = MailFormat.Html;
mail.Body = "this is my test email body.<br><b>this part is in bold</b>";
尝试一下
MailAddress mailfrom = new MailAddress ( "frommail@gmail.com" );
MailAddress mailto = new MailAddress ( "tomail@gmail.com" );
MailMessage newmsg = new MailMessage ( mailfrom, mailto );
newmsg.Subject = "Subject of Email";
newmsg.Body = "Body(message) of email";
////For File Attachment, more file can also be attached
Attachment att = new Attachment ( "G:\\code.txt" );
newmsg.Attachments.Add ( att );
SmtpClient smtps = new SmtpClient ( "smtp.gmail.com", 587 );
smtps.UseDefaultCredentials = false;
smtps.Credentials = new NetworkCredential ( "urmail@gmail.com", "urpwd" );
smtps.EnableSsl = true;
smtps.Send ( newmsg );
这篇关于从asp.net发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!