.net中使用outlook发送邮件

本文介绍如何在.NET环境中利用Outlook发送邮件。初学者可能会遇到一些困惑,但通过引入Inrerop.jmail库并创建邮件模板,可以实现邮件发送。在后台,通过特定的函数方法组装邮件,并使用File类的Ner_Email方法发送。成功发送后,可以添加弹窗提示和日志记录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

初次使用.net和outlool发送邮件,现在还是两眼一抹黑,有些原理还是搞不明白。

要引用Inrerop.jmail应用,我上传了资源在我的csdn里

首先建一个模板页

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <link href="css/Style.css?v=476" rel="stylesheet" />
    <style>
        .table-b table {
            border-right: 1px solid white;
            border-bottom: 1px solid white;
        }

            .table-b table td {
                border-left: 1px solid white;
                border-top: 1px solid white;
            }
    </style>

</head>
<body>
    <form id="form1" runat="server">
        <table style="width: 100%">
            <tr>
                <td class="tdborder"></td>
                <td>
                    <div>
                        <div id="divmain" style="text-align: center">
                            <div id="divfirst" style="background-color: #e0e0e0; margin: 0; text-align: left;">
                                <table style="width: 100%;background-color: #c0c0c0">
                                    <tr>
                                        <td style="width: 33%;text-align: left">
                                            <img src="images/logo320145.png" />
                                        </td>
                                        <td style="text-align: center;">
                                            <a>飞鲸卡商品订购单</a><br /><br />
                                            <a>iTourbagSoftCard Commodity order sheet</a>
                                        </td>
                                        <td style="width: 33%;text-align: right;">
                                            <a>途宝网络</a><br /><br />
                                            <a>Carlson Wagonlit Travel</a>
                                        </td>
                                    </tr>
                                </table>
                                <div class="table-b">
                                    <table style="width: 100%; background-color: #e0e0e0;">
                                        <tr>
                                            <td colspan="4">
                                                <br />
                                            </td>
                                        </tr>
                                        <tr>
                                            <td colspan="4" style="background-color: #ffffcc;">
                                                <a>套餐/The CamboMenu</a><a>
                                                    <span style="color: red">
                                                        marked with * are
                                                        mandatory(带*为必填项)
                                                    </span>
                                                </a>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="width: 160px;">
                                                国家或地区<span style="color: red">*</span>:<br>
                                                CountryorArea<span style="color: red">*</span>:
                                            </td>
                                            <td>
                                                #country#
                                            </td>
                                            <td style="width: 150px;">
                                                套餐天数<span style="color: red">*</span>:<br>
                                                Days<span style="color: red">*</span>:
                                            </td>
                                            <td>
                                                #days#
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                套 餐 内 容<span style="color: red">*</span>:<br>
                                                Content<span style="color: red">*</span>:
                                            </td>
                                            <td>
                                                #simcontent#
                                            </td>
                                            <td>
                                                充值参加价<span style="color: red">*</span>:<br>
                                                Price<span style="color: red">*</span>:
                                            </td>
                                            <td>
                                                #simprice#
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                用户名<span style="color: red">*</span>:<br>
                                                Username<span style="color: red">*</span>:
                                            </td>
                                            <td>
                                                #username#
                                            </td>
                                            <td style="width: 100px;">
                                                购买数量<span style="color: red">*</span>:<br>
                                                Quantity<span style="color: red">*</span>:
                                            </td>
                                            <td>
                                                #quantity#
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="width: 100px;">
                                                收货人<span style="color: red">*</span>:<br>
                                                Consignee<span style="color: red">*</span>:
                                            </td>
                                            <td>
                                                #consignee#
                                            </td>
                                            <td>
                                                联系方式<span style="color: red">*</span>:<br>
                                                Tel<span style="color: red">*</span>:
                                            </td>
                                            <td>
                                                #tel#
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                配送地址<span style="color: red">*</span>:<br>
                                                Addr<span style="color: red">*</span>:
                                            </td>
                                            <td>
                                                #addr#
                                            <td>
                                                配送方式<span style="color: red">*</span>:<br>
                                                Delivery<span style="color: red">*</span>:
                                            </td>
                                            <td>
                                                #delivery#
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                买家留言:<br>
                                                Remark:
                                            </td>
                                            <td colspan="3">
                                                #remark#
                                            </td>
                                        </tr>

                                    </table>
                                    <div id="divlast" style="text-align: right;">
                                        <div style="color: red;">
                                            <a><span style="color: red">*所有价格为参考价,实际价格可能会依据活动推广略有浮动调整</span></a><br />
                                            <a><span style="color: red">All prices are quoted prices, and the actual price may be adjusted slightly according to the promotion of the activities</span></a>
                                        </div>
                                        <div>
                                            <a>共计/Total:</a>
                                            #total#<a>RMB</a>
                                        </div>
                                    </div>
                                </div>

                            </div>
                        </div>

                    </div>
                </td>
                <td class="tdborder"></td>
            </tr>
        </table>
    </form>
</body>
</html>

在后台使用的函数方法里组装邮件

	var strFileCn = File.ReadFile(MapPath("order_model.html"), "utf-8");
	strFileCn = strFileCn.Replace("#country#", country);
	strFileCn = strFileCn.Replace("#days#", days);
	strFileCn = strFileCn.Replace("#simcontent#", simcontent);
	strFileCn = strFileCn.Replace("#simprice#", sales.price.ToString());
	strFileCn = strFileCn.Replace("#username#", sales.username);
	strFileCn = strFileCn.Replace("#quantity#", sales.quantity.ToString());
 	strFileCn = strFileCn.Replace("#consignee#", sales.receiver1);
	strFileCn = strFileCn.Replace("#tel#", sales.tel1);
	strFileCn = strFileCn.Replace("#addr#", sales.address1);
	strFileCn = strFileCn.Replace("#delivery#", sales.delivery);
	strFileCn = strFileCn.Replace("#remark#", sales.remark);
	strFileCn = strFileCn.Replace("#total#", sales.total.ToString());
	var att = new string[1];
	att[0] = "";
	var strRocFileName = "Adjunct/ROC/" + sales.orderid + ".htm";//这段不知道干嘛
	strRocFileName = Server.MapPath(strRocFileName);
 	try
 	{
	  File.WriteFile(strRocFileName, strFileCn, "utf-8");
 	}
	catch (Exception)
 	{
	   TabMsg.Rows[0].Cells[0].InnerHtml = "<script language='javascript'>alert('Error');</script>";
	   return;
	}//到这结束
	if (File.Net_Email(Sessions.sUser.email, "", "", strFileCn, "iTourbagSoftCard Commodity order sheet.", att, "", "") =="1")//发送邮件
 	{
	   TabMsg.Rows[0].Cells[0].InnerHtml =
 	"<script language='javascript'>alert('下单成功,我们会尽快帮您处理/Order Success');location.href='ordernew.aspx'</script>";//弹窗提示,方法自己定义
 	    File.SysLogAdd(1, _pfc.UserName, 1, "订单提交成功,系统自动发送邮件,我们会尽快帮您处理" + Sessions.sUser.email, Request.UserHostAddress);//记录日志
	}

首先读取模板页到strFileCn,然后使用Replace方法将模板页中的#?#元素一一替换掉,之后的 strRocFileName 的不知道干嘛用的。

使用File类里的Ner_Email方法将邮件发送过去,下面的弹窗提示,自己定义,也可以使用 ScriptManager.RegisterClientScriptBlock(this, GetType(), "aaa", "alert('Checkout Success(下单成功)');", true);这种方式。之后再写一个日志。file类的内容在下一篇里






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值