asp.net 郵件傳送提醒功能(接收方包括QQ郵箱等)
1、編寫一個通用的郵件傳送操作類:MailHelper.cs,程式碼如下:
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 SendeEmail(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;
}
//核心程式碼結束
}
}
2、Web頁面中使用及呼叫示例,程式碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MailSendDemo.aspx.cs" Inherits="demo_MailSendDemo" 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 language="javascript" type="text/javascript">
//獲取附件
function getAttachFile() {
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" />
<br />
<asp:Button ID="btnSend" runat="server" Text="傳送" OnClientClick="getAttachFile()" OnClick="btnSend_Click" /><br />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>
<asp:HiddenField ID="HiddenField1" runat="server" />
</form>
</body>
</html>
string a = mails.SendeEmail(TextBox1.Text, "你的郵箱號", "郵箱密碼", TextBox2.Text, TextBox4.Text, TextBox5.Text, filePath, TextBox3.Text);
這裡的在你實際使用的時候,替換成你的傳送方郵箱賬號和密碼即可。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class demo_MailSendDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSend_Click(object sender, EventArgs e)
{
//例項郵件幫助類,並進行郵件傳送
MailHelper mails = new MailHelper();
string filePath = HiddenField1.Value;
string a = mails.SendeEmail(TextBox1.Text, "你的郵箱賬號", "郵箱密碼", TextBox2.Text, TextBox4.Text, TextBox5.Text, filePath, TextBox3.Text);
Label1.Text = a;
}
}
===========================================================================
如果覺得對您有幫助,微信掃一掃支援一下:
相關文章
- golang傳送郵件(qq郵箱)Golang
- Java實現QQ郵件傳送郵件工具類Java
- spring boot配置QQ郵箱傳送郵件Spring Boot
- 電子郵件協議及GO傳送QQ郵件協議Go
- 收到263定時傳送郵件的傳送提醒
- ASP.NET之郵件傳送ASP.NET
- asp.net傳送郵件程式ASP.NET
- php 傳送郵件(以QQ為例)PHP
- JavaMail:java使用QQ郵箱傳送郵件簡單版。JavaAI
- 郵件傳送
- 傳送郵件
- Django實現傳送郵件功能Django
- Spring Boot實現傳送QQ郵件Spring Boot
- Linux Centos7傳送QQ郵件LinuxCentOS
- ASP.NET自動傳送郵件功能的實現ASP.NET
- SpringBoot整合Mail傳送郵件&傳送模板郵件Spring BootAI
- Bugfree郵件傳送設定(以QQ郵箱為例)
- JavaMailSender怎麼傳送163和qq郵件JavaAI
- ASP.net傳送郵件的例項ASP.NET
- 【Python】傳送郵件功能淺析Python
- Laravel 傳送郵件Laravel
- PHP傳送郵件PHP
- Django——郵件傳送Django
- java郵件傳送Java
- Laravel傳送郵件Laravel
- gmail傳送郵件AI
- Oracle郵件傳送Oracle
- java傳送郵件Java
- Powershell郵件傳送
- thinkphp 郵件傳送PHP
- centos 傳送郵件CentOS
- phpcms傳送郵件PHP
- 郵件的傳送
- 一次性解決python smtp 傳送outlook郵件,163郵件,qq郵件等等.Python
- 使用python傳送郵件和接收郵件Python
- Java Mail 郵件傳送(二):簡單封裝的郵件傳送JavaAI封裝
- asp.net定時傳送郵件總結ASP.NET
- laravel 郵件傳送功能使用說明Laravel