基于.Net的发邮件程序
.Net平台实现发邮件的功能实在是方便,废话不多说,直接上代码:
一.使用公司自己的SMTP服务器发邮件,不用输入邮箱Password.只能实现公司内部邮件的互发。
![](/Images/OutliningIndicators/ContractedBlock.gif)
Code
1 Imports System.Net.Mail
2 Imports System.Text
3
4 Public Class EmailInfo
5 Private _EmailSubject As String
6 Private _EmailBody As String
7 Private _EmailFrom As String
8 Private _EmailToList As String
9 Private _EmailCcList As String
10 Private _EmailBccList As String
11
12 Public Property EmailSubject() As String
13 Get
14 Return Me._EmailSubject
15 End Get
16 Set(ByVal value As String)
17 Me._EmailSubject = value
18 End Set
19 End Property
20
21 Public Property EmailBody() As String
22 Get
23 Return Me._EmailBody
24 End Get
25 Set(ByVal value As String)
26 Me._EmailBody = value
27 End Set
28 End Property
29
30 Public Property EmailFrom() As String
31 Get
32 Return Me._EmailFrom
33 End Get
34 Set(ByVal value As String)
35 Me._EmailFrom = value
36 End Set
37 End Property
38
39 Public Property EmailToList() As String
40 Get
41 Return Me._EmailToList
42 End Get
43 Set(ByVal value As String)
44 Me._EmailToList = value
45 End Set
46 End Property
47
48 Public Property EmailCcList() As String
49 Get
50 Return Me._EmailCcList
51 End Get
52 Set(ByVal value As String)
53 Me._EmailCcList = value
54 End Set
55 End Property
56
57 Public Property EmailBccList() As String
58 Get
59 Return Me._EmailBccList
60 End Get
61 Set(ByVal value As String)
62 Me._EmailBccList = value
63 End Set
64 End Property
65
66 Public Sub New()
67
68 End Sub
69 End Class
70
71 Public Class EmailApp
72
73 Private serverName As String
74
75 Public Function SendingEamil(ByVal theEmailInfo As EmailInfo) As String
76 Dim theMailMessage As New MailMessage
77 Dim smtpClientName As New SmtpClient(serverName)
78
79 theMailMessage.IsBodyHtml = True
80 theMailMessage.BodyEncoding = Encoding.UTF8
81
82 theMailMessage.Subject = theEmailInfo.EmailSubject
83 theMailMessage.From = New MailAddress(theEmailInfo.EmailFrom)
84 theMailMessage.Body = theEmailInfo.EmailBody
85 theMailMessage.Body += "<br />SMTP Server:" & serverName 'Please change your smtp server name
86
87 If theEmailInfo.EmailToList <> "" Then
88 theMailMessage.To.Clear()
89 For i As Integer = 0 To theEmailInfo.EmailToList.Split(","c).Length - 1
90 theMailMessage.To.Add(New MailAddress(theEmailInfo.EmailToList.Split(","c)(i)))
91 Next
92 End If
93
94 If theEmailInfo.EmailCcList <> "" Then
95 theMailMessage.CC.Clear()
96 For j As Integer = 0 To theEmailInfo.EmailCcList.Split(","c).Length - 1
97 theMailMessage.CC.Add(New MailAddress(theEmailInfo.EmailCcList.Split(","c)(j)))
98 Next
99 End If
100
101 If theEmailInfo.EmailBccList <> "" Then
102 theMailMessage.Bcc.Clear()
103 For k As Integer = 0 To theEmailInfo.EmailBccList.Split(","c).Length - 1
104 theMailMessage.Bcc.Add(New MailAddress(theEmailInfo.EmailBccList.Split(","c)(k)))
105 Next
106 End If
107 Try
108 smtpClientName.Send(theMailMessage)
109 Catch ex As Exception
110 Return "Error:" & ex.Message
111 End Try
112 Return "Mail has been sent Successfully!"
113 End Function
114 End Class
115
二.使用网络SMTP服务器
![](/Images/OutliningIndicators/ContractedBlock.gif)
Code
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using System.Net.Mail;
9 using System.Net.Sockets;
10 using System.Net;
11
14 namespace WindowsApplication1
15 {
16 public partial class Form1 : Form
17 {
18 public Form1()
19 {
20 InitializeComponent();
21 }
22
23 private void button1_Click(object sender, EventArgs e)
24 {
25 MailMessage message = new MailMessage();
26 message.IsBodyHtml = true;
27 message.From = new MailAddress(adcd@163.com);
28 message.To.Add(new MailAddress(aaa@yahoo.com));
29 message .Subject = "test";
30 message.Body = "test <br /> test";
31 SmtpClient nn = new SmtpClient("smtp.163.com");
32 nn.UseDefaultCredentials = false;
33 nn.Credentials = new NetworkCredential("abcd@163.com", "********");
34 nn.DeliveryMethod = SmtpDeliveryMethod.Network;
35 nn.Send(message);
38 }
39 }
40 }
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 Imports System.Net.Mail
2 Imports System.Text
3
4 Public Class EmailInfo
5 Private _EmailSubject As String
6 Private _EmailBody As String
7 Private _EmailFrom As String
8 Private _EmailToList As String
9 Private _EmailCcList As String
10 Private _EmailBccList As String
11
12 Public Property EmailSubject() As String
13 Get
14 Return Me._EmailSubject
15 End Get
16 Set(ByVal value As String)
17 Me._EmailSubject = value
18 End Set
19 End Property
20
21 Public Property EmailBody() As String
22 Get
23 Return Me._EmailBody
24 End Get
25 Set(ByVal value As String)
26 Me._EmailBody = value
27 End Set
28 End Property
29
30 Public Property EmailFrom() As String
31 Get
32 Return Me._EmailFrom
33 End Get
34 Set(ByVal value As String)
35 Me._EmailFrom = value
36 End Set
37 End Property
38
39 Public Property EmailToList() As String
40 Get
41 Return Me._EmailToList
42 End Get
43 Set(ByVal value As String)
44 Me._EmailToList = value
45 End Set
46 End Property
47
48 Public Property EmailCcList() As String
49 Get
50 Return Me._EmailCcList
51 End Get
52 Set(ByVal value As String)
53 Me._EmailCcList = value
54 End Set
55 End Property
56
57 Public Property EmailBccList() As String
58 Get
59 Return Me._EmailBccList
60 End Get
61 Set(ByVal value As String)
62 Me._EmailBccList = value
63 End Set
64 End Property
65
66 Public Sub New()
67
68 End Sub
69 End Class
70
71 Public Class EmailApp
72
73 Private serverName As String
74
75 Public Function SendingEamil(ByVal theEmailInfo As EmailInfo) As String
76 Dim theMailMessage As New MailMessage
77 Dim smtpClientName As New SmtpClient(serverName)
78
79 theMailMessage.IsBodyHtml = True
80 theMailMessage.BodyEncoding = Encoding.UTF8
81
82 theMailMessage.Subject = theEmailInfo.EmailSubject
83 theMailMessage.From = New MailAddress(theEmailInfo.EmailFrom)
84 theMailMessage.Body = theEmailInfo.EmailBody
85 theMailMessage.Body += "<br />SMTP Server:" & serverName 'Please change your smtp server name
86
87 If theEmailInfo.EmailToList <> "" Then
88 theMailMessage.To.Clear()
89 For i As Integer = 0 To theEmailInfo.EmailToList.Split(","c).Length - 1
90 theMailMessage.To.Add(New MailAddress(theEmailInfo.EmailToList.Split(","c)(i)))
91 Next
92 End If
93
94 If theEmailInfo.EmailCcList <> "" Then
95 theMailMessage.CC.Clear()
96 For j As Integer = 0 To theEmailInfo.EmailCcList.Split(","c).Length - 1
97 theMailMessage.CC.Add(New MailAddress(theEmailInfo.EmailCcList.Split(","c)(j)))
98 Next
99 End If
100
101 If theEmailInfo.EmailBccList <> "" Then
102 theMailMessage.Bcc.Clear()
103 For k As Integer = 0 To theEmailInfo.EmailBccList.Split(","c).Length - 1
104 theMailMessage.Bcc.Add(New MailAddress(theEmailInfo.EmailBccList.Split(","c)(k)))
105 Next
106 End If
107 Try
108 smtpClientName.Send(theMailMessage)
109 Catch ex As Exception
110 Return "Error:" & ex.Message
111 End Try
112 Return "Mail has been sent Successfully!"
113 End Function
114 End Class
115
二.使用网络SMTP服务器
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using System.Net.Mail;
9 using System.Net.Sockets;
10 using System.Net;
11
14 namespace WindowsApplication1
15 {
16 public partial class Form1 : Form
17 {
18 public Form1()
19 {
20 InitializeComponent();
21 }
22
23 private void button1_Click(object sender, EventArgs e)
24 {
25 MailMessage message = new MailMessage();
26 message.IsBodyHtml = true;
27 message.From = new MailAddress(adcd@163.com);
28 message.To.Add(new MailAddress(aaa@yahoo.com));
29 message .Subject = "test";
30 message.Body = "test <br /> test";
31 SmtpClient nn = new SmtpClient("smtp.163.com");
32 nn.UseDefaultCredentials = false;
33 nn.Credentials = new NetworkCredential("abcd@163.com", "********");
34 nn.DeliveryMethod = SmtpDeliveryMethod.Network;
35 nn.Send(message);
38 }
39 }
40 }