1. 程式人生 > >阿里雲ssl協議釋出qq郵件

阿里雲ssl協議釋出qq郵件

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="qq郵件.WebForm1" Async="true" %>

<!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>
</head>
<body>
    <form id="
form1" runat="server"> <div style="width:100%"> <h1>請輸入您的郵箱</h1> <table style="width:100%"><tr><td> <asp:TextBox ID="EmailText" runat="server" Width="100%" Height="50"></asp:TextBox></td></tr><tr><td> <asp:Button ID="
Button1" runat="server" Text="提交" OnClick="Button1_Click" Width="100%" /></td></tr></table> </div> </form> </body> </html>

前端程式碼,Async="true"
為非同步處理需要配置的。從控制元件拉一個文字框和按鈕,前端完成。

 protected void Button1_Click(object sender, EventArgs e)
        {
            
string title = ""; string text = ""; if (EmailText.Text == "[email protected]") { title = "你好哇,zss"; text = "我可能有點喜歡你了"; } else if (EmailText.Text == "[email protected]") { title = "飛兒子,你好"; text = "我是你爸爸"; } else { Random rd = new Random(); int i = rd.Next(1, 4);//(生成1~4之間的隨機數,不包括4) if (i == 1) { title = "曾經滄海難為水,除卻巫山不是雲"; text = "愛情是具有排他性的,曾經看過滄海那麼再看天底下的水便都變得平常了,看過巫山上的雲,其他地方的雲彩也不能稱為雲了。愛過一個人便如同弱水三千只取一瓢飲,意味著以後遇到再優秀的人也不能動心。"; } else if (i == 2) { title = "《德川家康》"; text = "人生有三大誘惑:少年時貪玩,荒廢了學業沒打下紮實的基礎;青年時貪情,在朋友和情人面前迷失了自我;中年時貪功,躺在功勞簿上驕傲自滿地犯懶。"; } else if (i == 3) { title = "寶貝"; text = "我不打算去見我超級想見的人了,我在等那個超級想見我的人。"; } } SenMailByQQemail(EmailText.Text, text, title); }

我在按鈕裡定義了,發生郵件的title標題,和text內容。用於隨機發送郵件。將定義好的資訊傳送到seMailByQQemail類。(接收方郵箱,內容,標題)。

public static bool SenMailByQQemail(string to, string body, string title)
        {
            bool retrunBool = false;
            MailMessage mail = new MailMessage();
            SmtpClient smtp = new SmtpClient();
            smtp.EnableSsl = true;  //設定加密連線   
            smtp.UseDefaultCredentials = false;
            string strFromEmail = "[email protected]";//你的郵箱  
            string strEmailPassword = "12456";//QQPOP3/SMTP授權碼
            try
            {
                mail.From = new MailAddress("sks<" + strFromEmail + ">");
                mail.To.Add(new MailAddress(to));
                mail.BodyEncoding = Encoding.UTF8;//文章編碼
                mail.IsBodyHtml = true;//文章是否html格式
                mail.SubjectEncoding = Encoding.UTF8;
                mail.Priority = MailPriority.Normal;
                mail.Body = body;
                mail.Subject = title;
                smtp.Host = "smtp.qq.com";
                smtp.Port = 587;//由於aliyun限制了25埠號,所以只能使用ssl協議587埠
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.Credentials = new System.Net.NetworkCredential(strFromEmail, strEmailPassword);
                //傳送郵件  
                smtp.Send(mail);   //同步傳送  
                retrunBool = true;
            }
            catch (Exception ex)
            {
                retrunBool = false;
            }
            // smtp.SendAsync(mail, mail.To); //非同步傳送 (非同步傳送時頁面上要加上Async="true" )  
            return retrunBool;
        }