使用c#在asp.net中阅读和发送邮件 [英] reading and sending mails in asp.net with c#
问题描述
我如何使用c#在asp.net中阅读和发送邮件? PLZ建议我一些代码...
T引用:他是微软.NET框架提供了两个名称空间,System.Net和System.Net.Sockets,用于管理互联网协议的实现,应用程序可以使用这些协议通过Internet发送或接收数据。 SMTP协议用于从C#发送电子邮件。 SMTP代表简单邮件传输协议。 C#使用System.Net.Mail命名空间发送电子邮件。我们可以实例化SmtpClient类并分配主机和端口。使用SMTP的默认端口是25,但它可能会有不同的邮件服务器。
以下C#源代码显示如何使用SMTP服务器从Gmail地址发送电子邮件。 Gmail SMTP服务器名称为smtp.gmail.com,使用发送邮件的端口为587,并且还使用NetworkCredential进行基于密码的身份验证。
SmtpClient SmtpServer =新的SmtpClient( smtp.gmail.com);
SmtpServer.Port = 587;
SmtpServer.Credentials =
新的System.Net.NetworkCredential(用户名,密码);
使用System;
使用System.Windows.Forms;
使用System.Net.Mail;
命名空间WindowsApplication1
{
公共部分类Form1:表格
{
public Form1()
{
InitializeComponent() ;
}
private void button1_Click(object sender,EventArgs e)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient(smtp.gmail.com);
mail.From = new MailAddress(your_email_address@gmail.com);
mail.To.Add(to_address);
mail.Subject =测试邮件;
mail.Body =这是用于测试来自GMAIL的SMTP邮件;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential(username,password);
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show(mail Send);
}
catch(exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
Quote:您必须提供必要的信息,例如您的Gmail用户名和密码等。
接收电子邮件:
1:{ www.codeproject.com/KB/IP/Pop3MailClient .aspx [ ^ ]}
2:{ C#.NET中的POP3客户端 [ ^ ]}
3:{ http:// dotnet。 mvps.org/dotnet/faqs/?id=email&lang=en [ ^ ]}
发送邮件:
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = smtp.gmail.com;
smtp.Port = 587 ;
smtp.EnableSsl = true ;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress,fromPassword);
smtp.Timeout = 20000 ;
}
// 将值传递给smtp对象
smtp。发送(fromAddress,toAddress,subject,body);
读取邮件:
(阅读邮件的来源是 这里 )
ASPX : -
< 表格 id = form1 runat = 服务器 >
< div >
来自:< asp:标签 ID = lblFrom runat = 服务器 文本 = / >
< br / >
主题:< asp:Label ID = lblSubject runat = server Text = / >
< < span class =code-leadattribute> br / >
正文:< asp:标签 ID = lblBody runat = server 文字 < span class =code-keyword> = / >
< / div >
< / form >
ASPX.cs : -
protected void Page_Load(object sender,EventArgs e)
{
if(! IsPostBack)
{
Pop3Client pop3Client =(Pop3Client)Session [Pop3Client];
int messageNumber = int.Parse(Request.QueryString [MessageNumber]);
消息消息= pop3Client.GetMessage(messageNumber);
MessagePart messagePart = message.MessagePart.MessageParts [0];
lblFrom.Text = message.Headers.From.Address;
lblSubject.Text = message.Headers.Subject;
lblBody.Text = messagePart.BodyEncoding.GetString(messagePart.Body);
}
}
HOW can I Read and send mails in asp.net using c#? plz suggest me some code...
解决方案TQuote:he Microsoft .NET framework provides two namespaces, System.Net and System.Net.Sockets for managed implementation of Internet protocols that applications can use to send or receive data over the Internet . SMTP protocol is using for sending email from C#. SMTP stands for Simple Mail Transfer Protocol . C# using System.Net.Mail namespace for sending email . We can instantiate SmtpClient class and assign the Host and Port . The default port using SMTP is 25 , but it may vary different Mail Servers .
The following C# source code shows how to send an email from a Gmail address using SMTP server. The Gmail SMTP server name is smtp.gmail.com and the port using send mail is 587 and also using NetworkCredential for password based authentication.
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
SmtpServer.Port = 587;
SmtpServer.Credentials =
new System.Net.NetworkCredential("username", "password");
using System; using System.Windows.Forms; using System.Net.Mail; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); mail.From = new MailAddress("your_email_address@gmail.com"); mail.To.Add("to_address"); mail.Subject = "Test Mail"; mail.Body = "This is for testing SMTP mail from GMAIL"; SmtpServer.Port = 587; SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password"); SmtpServer.EnableSsl = true; SmtpServer.Send(mail); MessageBox.Show("mail Send"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } }
Quote:You have to provide the necessary information like your gmail username and password etc.
To Receive Email:
1: {www.codeproject.com/KB/IP/Pop3MailClient.aspx[^]}
2:{A POP3 Client in C# .NET[^]}
3:{http://dotnet.mvps.org/dotnet/faqs/?id=email&lang=en[^]}
Send Mail :
var smtp = new System.Net.Mail.SmtpClient(); { smtp.Host = "smtp.gmail.com"; smtp.Port = 587; smtp.EnableSsl = true; smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; smtp.Credentials = new NetworkCredential(fromAddress, fromPassword); smtp.Timeout = 20000; } // Passing values to smtp object smtp.Send(fromAddress, toAddress, subject, body);
Read Mail :
(source for reading mail is from here)
ASPX :-
<form id="form1" runat="server"> <div> From: <asp:Label ID="lblFrom" runat="server" Text="" /> <br /> Subject: <asp:Label ID="lblSubject" runat="server" Text="" /> <br /> Body: <asp:Label ID="lblBody" runat="server" Text="" /> </div> </form>
ASPX.cs :-
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Pop3Client pop3Client = (Pop3Client)Session["Pop3Client"]; int messageNumber = int.Parse(Request.QueryString["MessageNumber"]); Message message = pop3Client.GetMessage(messageNumber); MessagePart messagePart = message.MessagePart.MessageParts[0]; lblFrom.Text = message.Headers.From.Address; lblSubject.Text = message.Headers.Subject; lblBody.Text = messagePart.BodyEncoding.GetString(messagePart.Body); } }
这篇关于使用c#在asp.net中阅读和发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!