使用asp.net和C#发送电子邮件
问题描述:
大家好,
我正在尝试使用asp.net和c#发送电子邮件,但它显示发送电子邮件失败。
i拥有域名.ca
重要。
谢谢
Naveen
Hi all,
I am trying to send email using asp.net and c#, but it shows failure sending email.
i have the domain with .ca
Does it matter.
Thanks
Naveen
答
Public Function SendMail(sender As Object, e As EventArgs) As String
Dim retVal As String = Nothing
Dim urlURI As String = HttpContext.Current.Request.Url.AbsoluteUri
Dim urlPath As String = HttpContext.Current.Request.Url.AbsolutePath
Dim myServerName As String = Strings.Left(urlURI, urlURI.Length - urlPath.Length)
Dim clientMail As String = ' This is your client e-mail
Dim myCred As Net.NetworkCredential = New Net.NetworkCredential
Dim MailConf As New Net.Mail.SmtpPermissionAttribute(System.Security.Permissions.SecurityAction.Assert)
myCred.Domain = 'here is you mail Domain... your domain
myCred.Password = ' here is the password from the above account
myCred.UserName = ' here is an account on your mail server on which you'll Logged in
Dim myGuid As String = 'If you want to reply in this mail then put his Guid record from SQL server
'Create a new MailMessage object and specify the"From" and "To" addresses
Dim Email As New System.Net.Mail.MailMessage(myCred.UserName, clientMail)
Email.Subject = "Verification Mail from ......."
Dim messageString As String = 'Your Message string on the e-mail body
Email.Body = messageString
Dim mailClient As New System.Net.Mail.SmtpClient()
'This object stores the authentication values
Dim basicAuthenticationInfo As _
New System.Net.NetworkCredential(myCred.UserName, myCred.Password)
mailClient.Host = myCred.Domain
mailClient.UseDefaultCredentials = False
mailClient.Credentials = basicAuthenticationInfo
mailClient.Send(Email)
reader.Dispose()
Return retVal
End Function
嗯这是我在发送电子邮件问题上的建议
Well that's my propose in your issue with sending e-mail
您可以使用gmail帐户来实现此目的
发送您要发送的邮件的参数和电子邮件接收方的电子邮件地址为无效方法
You can use a gmail acount for that purpose
Send parameters for the message you want to send and the e-mail address for the receiver of the e-mail to a void method
private void SendNotification ( string message, string reciever )
{
string userName = "yourName@gmail.com"; //insert the usernme for the mail account
string passWord = "yourPassword"; //insert corrosponding password
string fromText = "DoNotReply@domain.com"; //Whatever to identify the Sender
string toText = receiver;
string subjectText = "e-mail header text";
string bodyText = "email body text"
SmtpClient smtpClient = new SmtpClient ( "smtp.gmail.com", 587 );
smtpClient.EnableSsl = true;
smtpClient.Timeout = 10000;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential ( userName, passWord );
MailMessage mail = new MailMessage ( fromText, toText, subjectText, bodyText );
mail.BodyEncoding = UTF8Encoding.UTF8;
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
smtpClient.Send ( mail );
}
试试这段代码:
你必须使用像这样的SMTP服务GMAIL / YAHOO。
Try this code:
you have to use SMTP service like GMAIL/YAHOO.
public void GetMail()
{
NetworkCredential cred = new NetworkCredential("name@domail.com", "Password");
MailMessage msg = new MailMessage();
msg.To.Add("name@domail.com");
msg.Subject = "Welcome";
msg.Body = "You Have Successfully sent mail!!!";
msg.From = new MailAddress("name@domail.com"); // Your Email Id
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
SmtpClient client1 = new SmtpClient("smtp.mail.yahoo.com", 465);
client.Credentials = cred;
client.EnableSsl = true;
client.Send(msg);
}