我需要从我的应用程序发送邮件消息,仍然有连接问题。请帮我看看这段代码。 [英] i need to send a mail msg from my application, still having prob with connection. please help me review this code.
本文介绍了我需要从我的应用程序发送邮件消息,仍然有连接问题。请帮我看看这段代码。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
//这些名称空间名为
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Net;
使用 System.Net.Mail;
使用 System.Net.Mime;
使用 System.Deployment;
使用 System.Threading;
使用 System.ComponentModel;
尝试
{
MailMessage mail = new MAILMESSAGE();
// 设置From,To和CC
mail.From = new MailAddress( info@wlcfoleh.com , MyWeb网站);
mail.To.Add( new MailAddress( eruoghene507@gmail.com跨度>));
mail.CC.Add( new MailAddress( MyEmailID@gmail.com跨度>));
mail.Body = msgtext.Text;
SmtpClient smtpClient = new SmtpClient( smtp.gmail.com, 25 );
smtpClient.Credentials = new System.Net.NetworkCredential( eruoghene507@gmail.com, 这很有趣跨度>);
smtpClient.UseDefaultCredentials = true ;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = false ;
smtpClient.Send(mail);
Response.Write( Email Sent);
}
catch (例外q)
{
Label12.Text = 消息: + q.Message + 来源: + q.Source + 堆栈跟踪: + q 。堆栈跟踪;
}
解决方案
将代码更改为
< pre lang =c#> MailMessage mail = new MailMessage();
// 设置From,To和CC
mail.From = new MailAddress( MyEmailID@gmail.com , MyWeb网站);
mail.To.Add( new MailAddress( eruoghene507@gmail.com跨度>));
mail.CC.Add( new MailAddress( MyEmailID@gmail.com跨度>));
mail.Body = msgtext.Text;
SmtpClient smtpClient = new SmtpClient( smtp.gmail.com, 587 );
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
smtpClient.EnableSsl = true ;
smtpClient.UseDefaultCredentials = false ;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Credentials = new System.Net.NetworkCredential( eruoghene507@gmail.com, 这很有趣);
smtpClient.Send(mail);
Response.Write( Email Sent);
希望上述更改有所帮助。
请尝试使用相同的代码进行工作。 。
MailMessage msf = new MailMessage(samatha507@gmail.com,samatha507 @gmail.com,测试邮件,无论);
msf.IsBodyHtml = false;
NetworkCredential ne = new NetworkCredential(samatha507@gmail.com,mypassword);
SmtpClient sc = new SmtpClient(smtp.gmail.com,587);
sc.UseDefaultCredentials = false;
sc.Credentials = ne;
sc.EnableSsl = true;
sc.Send(msf);
Response.Write(邮件发送成功);
//these are namespaces i called
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Deployment;
using System.Threading;
using System.ComponentModel;
try
{
MailMessage mail = new MailMessage();
//Setting From , To and CC
mail.From = new MailAddress("info@wlcfoleh.com", "MyWeb Site");
mail.To.Add(new MailAddress("eruoghene507@gmail.com"));
mail.CC.Add(new MailAddress("MyEmailID@gmail.com"));
mail.Body = msgtext.Text;
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 25);
smtpClient.Credentials = new System.Net.NetworkCredential("eruoghene507@gmail.com", "this is fun");
smtpClient.UseDefaultCredentials = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = false;
smtpClient.Send(mail);
Response.Write("Email Sent");
}
catch (Exception q)
{
Label12.Text = "Message: " + q.Message + "Source: " + q.Source + "Stack Trace: " + q.StackTrace;
}
解决方案
Change the code to
MailMessage mail = new MailMessage(); //Setting From , To and CC mail.From = new MailAddress("MyEmailID@gmail.com", "MyWeb Site"); mail.To.Add(new MailAddress("eruoghene507@gmail.com")); mail.CC.Add(new MailAddress("MyEmailID@gmail.com")); mail.Body = msgtext.Text; SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; smtpClient.EnableSsl = true; smtpClient.UseDefaultCredentials = false; smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; smtpClient.Credentials = new System.Net.NetworkCredential("eruoghene507@gmail.com", "this is fun"); smtpClient.Send(mail); Response.Write("Email Sent");
Hope the above changes help.
Please try like this it is working.am using same code. MailMessage msf = new MailMessage("samatha507@gmail.com", "samatha507@gmail.com", "test mail", "no matter"); msf.IsBodyHtml = false; NetworkCredential ne = new NetworkCredential("samatha507@gmail.com", "mypassword"); SmtpClient sc = new SmtpClient("smtp.gmail.com", 587); sc.UseDefaultCredentials = false; sc.Credentials = ne; sc.EnableSsl = true; sc.Send(msf); Response.Write("mail send successfully");
这篇关于我需要从我的应用程序发送邮件消息,仍然有连接问题。请帮我看看这段代码。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文