Jmail組件寄送電子郵件

來源:互聯網
上載者:User

關於Jmail的使用介紹網上已經不少,我把自己的體驗紀錄一下,既協助跟我一樣的初學者也是個積累。
    Jmail的使用主要有以下幾點:
    一. 安裝:要在機器上安裝Jmail組件,其實就是一個“Jmail.dll”,一般會被安裝到

C:\windows\system32\目錄中。如果下載到的僅僅是Jmail.dll這個檔案,就可以手動放到這個目錄,不過別的目

錄應該也可以,我沒試過。
    二. 註冊:自動安裝一般同時會自動註冊,無需關心這個。但是下載的僅僅是Jmail.dll這個檔案,那麼必須

自己手動註冊。註冊的方法如下:在命令列視窗裡運行命令“regsvr c:\windows\system32\jmail.dll”,當然

檔案路徑自己指定。
    三. 引用:把jmail.dll複製到Web網站的bin目錄裡,然後給網站添加COM引用。
    四. 寫代碼:類似如下,記得引用命名空間。

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

 

using jmail;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        jmail.MessageClass mail = new jmail.MessageClass();

        DateTime t = DateTime.Now;
        string subject = "Jmail test! " + t.ToString();
        string body = "Hello world!<br/>";
       
       
        string fromemail = "mymail@126.com";
        string toemail = "mail@china.com";
        //silent屬性:如果設定為true,jmail不會拋出例外錯誤. jmail. send( () 會根據操作結果返回true

或false
        mail.Silent = true;
        //jmail建立的日誌,前提loging屬性設定為true
        mail.Logging = true;
        //字元集,預設為"us-ascii"gb2312
        mail.Charset = "UTF-8";
        mail.Encoding = "BASE64";
        //信件的contentype. 預設是"text/plain") : 字串如果你以html格式發送郵件, 改為"text/html"

即可。
        mail.ContentType = "text/html";
        //添加收件者
        mail.AddRecipient(toemail, "", "");
        mail.From = fromemail;
        //寄件者郵件使用者名
        mail.MailServerUserName = "mymail@126.com";
        //寄件者郵件密碼
        mail.MailServerPassWord = "123";
        //設定郵件標題
        mail.Subject = subject;
        //郵件添加附件,(多附件的話,可以再加一條jmail.addattachment( "c:\\test.jpg",true,null);)就

可以搞定了。[注]:加了附件,講把上面的jmail.contenttype="text/html";刪掉。否則會在郵件裡出現亂碼
        //mail.AddAttachment("c:\\test.jpg", true, null);
        //郵件內容
        mail.Body = body + t.ToString();
        //jmail發送的方法
        if (mail.Send("smtp.126.com", false))
        {
            Response.Write("<script language='javascript'>alert('OK!')</script>");
        }
        else
        {
            Response.Write("<script language='javascript'>alert('False!')</script>");
            Response.Write(mail.ErrorCode.ToString() + "<br/>" + mail.ErrorMessage.ToString() +

"<br/>" + mail.ErrorSource.ToString() + "<br/>");
        }
        mail.Close();
    }
}

//遇到的錯誤
通常我們遇到的錯誤是:'The message was undeliverable. All servers failed to receive the message ',這其實是JMAIL返回的錯誤,並不是ASP代碼產生的,根本原因是MAIL SERVER拒絕了JMAIL的請求.

究其原因,是那些伺服器不提供SMTP服務或者沒有開啟smtp服務;或是在伺服器端開啟了'禁止漏洞轉發匿名郵件服務'選項,也就是說不在其允許的IP段或指定範圍內的空間裡的程式是無法使用其SMTP服務的。解決方案:使用支援smtp的郵件伺服器. 使用支援外來jmail申請驗證身份,發送郵件的郵件伺服器。 最好:使用自己的待遇smtp功能的企業郵局。因為外面的免費的郵局可能會有一些特殊設定,不如防止垃圾郵件,防止盜用郵件身份等等!

Jmail發送首先要通過郵件伺服器驗證。
如果你的伺服器不支援SMTP或者你的帳號不能使用SMTP服務
那麼就無法發送。

163以前的使用者預設是開通POP和SMTP服務的
但新使用者都不開通,需要付費才能使用。

要想確定某一郵箱是否可以使用POP和SMTP,你可以用foxmail等郵件軟體看能否收取該郵箱信件。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.