分享一个asp.net发送邮件实例


本文摘自PHP中文网,作者怪我咯,侵删。

这篇文章主要介绍了asp.net发送邮件示例分享,需要的朋友可以参考下

mailhelper -------mail帮助类

代码如下:

1

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

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mail;

/// <summary>

///mailhelper 的摘要说明

/// </summary>

public class mailhelper

{

    public mailhelper()

    {

        //

        //TODO: 在此处添加构造函数逻辑

        //

    }

    /// <summary>

    /// 邮件发送操作

    /// </summary>

    /// <param name="Addressee">收件人地址</param>

    /// <param name="From">发件人地址</param>

    /// <param name="sendpassword">发件人密码</param>

    /// <param name="Copy">抄送人地址</param>

    /// <param name="secret">密送人地址</param>

    /// <param name="Subject">发送主题</param>

    /// <param name="Attachment">附件信息</param>

    /// <param name="Body">邮件内容</param>

    public string SendeEmal(string Addressee, string From, string sendpassword, string Copy, string secret, string Subject, string Attachment, string Body)

    {

        MailMessage objMailMessage;

        MailAttachment objMailAttachment;

 

        // 创建邮件消息

        objMailMessage = new MailMessage();

        //发件人EMAIL

        objMailMessage.From = From;//源邮件地址

        //收件人EMAIL

        objMailMessage.To = Addressee; //目的邮件地址

        //邮件抄送

        objMailMessage.Cc = Copy;

        //邮件misong

        objMailMessage.Bcc = secret;

 

        //邮件主题

        objMailMessage.Subject = Subject; //发送邮件的标题

        //邮件内容

        objMailMessage.Body = Body;//发送邮件的内容

        // 创建一个附件对象

        if (Attachment != "")

        {

            objMailAttachment = new MailAttachment(Attachment);//发送邮件的附件 c:\\test.txt

            objMailMessage.Attachments.Add(objMailAttachment);//将附件附加到邮件消息对象中

        }

        //接着利用SMTP来发送邮件,需要使用Microsoft .NET Framework SDK v1.1和它以上的版本

        //基本权限

        objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");

        //用户名

        string name = From.Substring(0, From.IndexOf('@'));

        objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", name);

        //密码

        objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", sendpassword);

        //如果没有上述三行代码,则出现如下错误提示:服务器拒绝了一个或多个收件人地址。服务器响应为: 554 : Client host rejected: Access denied

        //SMTP地址     

        string smtp = "smtp." + From.Substring(From.IndexOf('@') + 1);

        SmtpMail.SmtpServer = "smtp." + From.Substring(From.IndexOf('@') + 1);

        //开始发送邮件

        try

        {

            SmtpMail.Send(objMailMessage);

            return "邮件发送成功!";

        }

        catch (System.Net.Mail.SmtpException ex)

        {

            return ex.Message;

        }

        //核心代码结束

    }

}

然后下来是自己做的一个demo--

前台

代码如下:

1

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

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="mail.aspx.cs" Inherits="information_mail"

    ValidateRequest="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

    <script src="../Style/jquery/jquery.js" type="text/javascript"></script>

    <script src="../Style/jquery/jquery.validate.js" type="text/javascript"></script>

    <script language="javascript" type="text/javascript">

        function gei() {

            var file_value = document.getElementById("File1").value;

            document.getElementById("HiddenField1").value = file_value;

        }

    </script>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        发给:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />

        抄送:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />

        密送:<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />

        主题:<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox><br />

        内容:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />

        附件:<input id="File1" type="file" />

        <%--<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>--%>

        <br />

        <asp:Button ID="Button1" runat="server" Text="发送" OnClientClick="gei()" OnClick="Button1_Click" /><br />

        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>

    </div>

    <asp:HiddenField ID="HiddenField1" runat="server" />

    </form>

</body>

</html>


后台:

代码如下:

1

2

3

4

5

6

7

protected void Button1_Click(object sender, EventArgs e)

    {        //实例邮件帮助类

        mailhelper mails = new mailhelper();

        string filePath = HiddenField1.Value;

        string a = mails.SendeEmal(TextBox1.Text, "邮件账号", "邮件密码", TextBox2.Text, TextBox4.Text, TextBox5.Text, filePath, TextBox3.Text);

        Label1.Text = a;

}

以上就是分享一个asp.net发送邮件实例的详细内容!

相关阅读 >>

画图形验证码的asp代码实例

asp.net core实例教程之配置

asp.net连接access数据库相对路径写法

asp.net中实现des加密与解密md5加密功能介绍

asp.net网站发布的过程详解

asp.net常用函数总结

分享两种asp.net网站发布时的遇到的问题及解决方案

一段asp.net des加密解密的代码

怎么操作 asp.net web api ?

asp.net mvc 5改进了基于过滤器的身份验证

更多相关阅读请进入《asp.net》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...