今天有一个模块需要自动发送邮件的功能,就随便写了一个,记录一下作为积累。
一、首先需要配置web.config文件:
1
<system.net>
2
<mailSettings>
3
<smtp from="Emailname">
4
<network host="smtp.163.com" userName="Emailname" password="Emailpassword"
5
port="25" defaultCredentials="false"/>
6
</smtp>
7
</mailSettings>
8
</system.net>

2

3

4

5

6

7

8

二、然后编写发送邮件的函数:
1
Asp.net 自动发送邮件的方法
2
今天有一个模块需要自动发送邮件的功能,就随便写了一个,记录一下作为积累。
3
4
5
一、首先需要配置web.config文件:
6
7
8
<system.net>
9
<mailSettings>
10
<smtp from="Emailname">
11
<network host="smtp.163.com" userName="Emailname" password="Emailpassword"
12
port="25" defaultCredentials="false"/>
13
</smtp>
14
</mailSettings>
15
</system.net>
16
17
18
二、然后编写发送邮件的函数:
19
20
21
//// <summary>
22
/// 邮件发送方法(带附件)
23
/// </summary>
24
/// <param name="mailto">收件人地址。如:receiver@163.com</param>
25
/// <param name="mailsubject">邮件标题</param>
26
/// <param name="mailbody">邮件正文</param>
27
/// <param name="mailFrom">邮件发送人地址。如:sender@163.com</param>
28
/// <param name="list">附件路径</param>
29
/// <returns></returns>
30
public bool MySendMail(string mailto, string mailsubject, string mailbody, string mailFrom, ArrayList list)
31
{
32
try
33
{
34
//邮件发送人地址
35
System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress(mailFrom);
36
//如test@163.com,初步测试,用test@sina.com不行,用163的邮件服务器,就必须用163邮箱的用户名
37
//收件人地址
38
System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress(mailto);//如test@tom.com
39
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(from, to);
40
mail.Subject = mailsubject;
41
mail.Body = mailbody;
42
//以下设置服务器
43
System.Net.Mail.SmtpClient mySmth = new System.Net.Mail.SmtpClient();
44
//以下为增加附件
45
int count = list.Count;
46
for (int i = 0; i < count; i++)
47
{
48
System.Net.Mail.Attachment data = new System.Net.Mail.Attachment(list[i].ToString());
49
mail.Attachments.Add(data);
50
}
51
mySmth.Send(mail);
52
mail.Dispose();
53
return true;
54
}
55
catch
56
{
57
return false;
58
}
59
}
60
61
62
三、最后就是对函数的调用了:
63
64
65
//自动发送邮件
66
string mailSubject = "会员注册确认函";
67
string mailBody = "正文内容。";
68
string mailFrom = ConfigurationManager.AppSettings["SendMail"];
69
ArrayList List = new ArrayList();
70
List.Add(Server.MapPath(ConfigurationManager.AppSettings["SendMailText"]));
71
if (MySendMail(this.txtEmail.Text, mailSubject, mailBody, mailFrom, List))
72
{
73

74
//发送成功,进行相应处理
75
}
76
else
77
{
78

79
//发送失败,进行相应处理
80
return;
81
}
82
83

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73


74

75

76

77

78


79

80

81

82

83

三、最后就是对函数的调用了:
1
//自动发送邮件
2
string mailSubject = "会员注册确认函";
3
string mailBody = "正文内容。";
4
string mailFrom = ConfigurationManager.AppSettings["SendMail"];
5
ArrayList List = new ArrayList();
6
List.Add(Server.MapPath(ConfigurationManager.AppSettings["SendMailText"]));
7
if (MySendMail(this.txtEmail.Text, mailSubject, mailBody, mailFrom, List))
8
{
9

10
//发送成功,进行相应处理
11
}
12
else
13
{
14

15
//发送失败,进行相应处理
16
return;
17
}

2

3

4

5

6

7

8

9


10

11

12

13

14


15

16

17
