在項目中使用JMail發送郵件

來源:互聯網
上載者:User

標籤:http   使用   os   strong   檔案   io   art   代碼   

1.添加JMail組件:在vs的方案總管中,右鍵添加引用,選擇瀏覽,選擇jmail.dll檔案,在Bin檔案夾中就可以看到添加的JMail組件.

 

2.註冊JMail組件:開始-->運行-->輸入如:regsvr32 D:\DotNet相關外掛程式和指令碼\jmail.dll

 

Regsvr32 解釋:
這個命令列工具將 dll 檔案註冊為註冊表中的命令組成。
文法
regsvr32 【/u】 【/s】 【/n】 【/i【:cmdline】】 dllname
參數
/u 
未註冊伺服器。 
/s 
指定 regsvr32 安靜運行,且不顯示任何訊息框。 
/n 
指定不調用 DllRegisterServer。此選項必須與 /i 共同使用。 
/i:cmdline 
調用 DllInstall 將它傳遞到可選的 【cmdline】。在與 /u 共同使用時,它調用 dll 卸載。 
dllname 
指定要註冊的 dll 檔案名稱。 
/? 
在命令提示字元顯示協助。

 

完成1,2部操作後,我們來編寫代碼:

 

介面參考:

C#代碼  
    1. //點擊發送郵件  
    2.   protected void btnTranSmit_Click(object sender, EventArgs e)  
    3.   {  
    4.       try  
    5.       {  
    6.           //調用發送郵件的方法  
    7.           SendEmail(txtSender.Text, txtSenderName.Text, txtTransmitName.Text, txtTransmitPassword.Text, txtReceiver.Text, txtSubject.Text, txtContent.Text, txtServer.Text);  
    8.       }  
    9.       catch (Exception ex)  
    10.       {  
    11.           Response.Write(ex.Message);  
    12.       }  
    13.   }  
    14.   
    15.   
    16.   /// <summary>  
    17.   /// 使用Jmail發送郵件的方法  
    18.   /// </summary>  
    19.   /// <param name="sender">寄件者郵箱</param>  
    20.   /// <param name="senderName">寄件者姓名</param>  
    21.   /// <param name="TransmitName">發送郵件人的郵箱的登入名稱</param>  
    22.   /// <param name="TransmitPassWord">發送郵件人的郵箱密碼</param>  
    23.   /// <param name="Receiver">收件者的地址</param>  
    24.   /// <param name="subject">郵件的主題</param>  
    25.   /// <param name="Content">郵件的內容</param>  
    26.   /// <param name="ServerHost">伺服器的地址</param>  
    27.   private void SendEmail(String sender, String senderName, String TransmitName, String TransmitPassWord, String Receiver, String subject, String Content, String ServerHost)  
    28.   {  
    29.       //建立Jmail對象  
    30.       MessageClass jMessage = new MessageClass();  
    31.   
    32.       //設定編碼  
    33.       jMessage.Charset = "GB2312";  
    34.   
    35.       //設定郵件標頭不使用ISO-8859-1的編碼  
    36.       jMessage.ISOEncodeHeaders = false;  
    37.   
    38.       //設定發送的郵件的郵箱地址  
    39.       jMessage.From = sender;  
    40.   
    41.       //設定寄件者姓名  
    42.       jMessage.FromName = senderName;  
    43.   
    44.       //設定主題  
    45.       jMessage.Subject = subject;  
    46.   
    47.       //設定發送郵件的郵箱或者郵件伺服器登陸名  
    48.       jMessage.MailServerUserName = TransmitName;  
    49.   
    50.       //設定發送郵件的郵箱或者郵件伺服器密碼  
    51.       jMessage.MailServerPassWord = TransmitPassWord;  
    52.   
    53.       //添加內送郵件的郵箱地址  
    54.       jMessage.AddRecipient(Receiver, "", "");  
    55.   
    56.       //設定發送郵件內文  
    57.       jMessage.Body = Content;  
    58.   
    59.       //判斷上傳控制項是否為空白  
    60.       if (fluAttachMent.HasFile)  
    61.       {  
    62.           String fileName = fluAttachMent.PostedFile.FileName;  
    63.           //添加附件  
    64.           jMessage.AddAttachment(fileName, true, "");  
    65.       }  
    66.   
    67.       //發送郵件  
    68.       if (jMessage.Send(ServerHost, false))  
    69.       {  
    70.           Response.Write("<script>alert(‘發送成功!‘);</script>");  
    71.       }  
    72.       else  
    73.       {  
    74.           Response.Write("<script>alert(‘發送失敗!‘);</script>");  
    75.       }  
    76.   
    77.   }  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.