Jmail收发邮件(完整源码)

原创 2007年10月15日 16:40:00

这段时间在做一个邮件收发系统!!由于刚接触.所以走了不少的弯路.很好得到一些朋友的指点,终于一些关键技术点做完了!为了避免大家跟我一样走弯路.我特地把关键代码贴出来.大家一起学习!

asp.net(vb.net)+sql2000+jmail

  首先把功能介绍一下:主要介绍一点的就是.发送邮件这块功能.支持地址薄选地址(弹出窗口返回值).也支持用户自己填写email地址,同时支持群发(也就是用户组),主要代码如下:

前台:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="sendmail.aspx.vb" Inherits="sendmail" %>

<%@ Register Assembly="FreeTextBox" Namespace="FreeTextBoxControls" TagPrefix="FTB" %>

<!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>
     <link href ="body.css" rel ="Stylesheet" type ="text/css" />
     <script language ="javascript" type ="text/javascript" >
     window.onload =function()
     {
     document.getElementById ("div1").style.visibility ="hidden";
     }
    
     function showcalendar()       
     {
     document.getElementById ("div1").style.visibility ="visible";
     }
     var sendto="";
     var cto=""
     var sto=""
     var attname=""
     function showmailaddress()     ''这里很重要,利用showmailDialog获取值
     {
     window.showModalDialog ('mailaddress.aspx',window,'resizable:yes;dialogWidth:640px;dialogHeight:550px;');
     }
    
     function getmailaddress()
     {
         if(document.getElementById ("sendto").value!="")
       {
       document.getElementById ("sendto").value+=";";
       }
           if(document.getElementById ("cto").value!="")
       {
       document.getElementById ("cto").value+=";";
       }
           if(document.getElementById ("sto").value!="")
       {
       document.getElementById ("sto").value+=";";
       }
     document.getElementById ("sendto").value+=sendto;
     document.getElementById ("cto").value+=cto;
     document.getElementById ("sto").value+=sto;
     }
    
     function showmailatt()  
     {
      window.showModalDialog ('mailatt.aspx',window,'center:yes;dialogHeight:10px;dialogWidth:400px;');
     }
    
     function getattname()     ''获取附件
     {
     var att=document.getElementById ("attname");
    
      if(att.value!="")
       {
       att.value+=";";
       }
       att.value+=attname;
     }
    
     </script>
     <link href ="body.css" rel ="Stylesheet" type ="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div align ="center">
        <table  width ="100%" >
            <tr>
                <td align ="left" >
                <label id="label1" >收件人</label><img style=" cursor :hand " onclick ="showmailaddress();" src ="TreeLineImages/mailbox.bmp" />
                    <input id="sendto" style ="width :80%;" type="text" runat ="server" />
            </tr>
            <tr>
                <td  align ="left">
                 <label id="label2" >
                     抄&nbsp; &nbsp; 送</label><img style=" cursor :hand " onclick ="showmailaddress();" src ="TreeLineImages/mailbox.bmp" />
                    <input id="cto" style ="width :80%;" type="text" runat ="server" />
                </td>
            </tr>
            <tr>
                <td align ="left" >
                 <label id="label3" >
                     密&nbsp;&nbsp; &nbsp;送</label><img style=" cursor :hand " onclick ="showmailaddress();" src ="TreeLineImages/mailbox.bmp" />
                    <input id="sto" style ="width :80%;" type="text" runat ="server" />
                </td>
            </tr>
            <tr>
                <td align ="left"  >
                 <label id="label4" >優先級別:</label>
                 <asp:DropDownList ID="graddl" runat="server" AutoPostBack="True">
                         <asp:ListItem Value="1">高</asp:ListItem>
                         <asp:ListItem Value="3">中</asp:ListItem>
                         <asp:ListItem Value="5">低</asp:ListItem>
                     </asp:DropDownList>
                    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
                    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
                    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
                    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;
               
                    <label id="label5" >發送時間:</label><input id="sendtime" runat ="server"  type="text" />
                    <img style=" cursor :hand " onclick ="showcalendar();" runat ="server" src ="images/calender.BMP" id="IMG1" />
                    <div id="div1" style="z-index: 101; left: 616px; width: 100px; position: absolute; top: 128px;
                        height: 100px">
                        <asp:Calendar ID="Calendar1" runat="server" BackColor="White" OnSelectionChanged ="Calendar1_SelectionChanged"   BorderColor="#999999" Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" Height="180px" Width="200px" CellPadding="4" DayNameFormat="Shortest">
                            <SelectedDayStyle BackColor="#666666" ForeColor="White" Font-Bold="True" />
                            <TodayDayStyle BackColor="#CCCCCC" ForeColor="Black" />
                            <OtherMonthDayStyle ForeColor="#808080" />
                            <NextPrevStyle VerticalAlign="Bottom" />
                            <DayHeaderStyle Font-Bold="True" Font-Size="7pt" BackColor="#CCCCCC" />
                            <TitleStyle BackColor="#999999" BorderColor="Black" Font-Bold="True" />
                            <SelectorStyle BackColor="#CCCCCC" />
                            <WeekendDayStyle BackColor="#FFFFCC" />
                        </asp:Calendar>
                    </div>
            </tr>
            <tr>
                <td align ="left" >
                <label id="label6" >
                     附&nbsp; &nbsp;件</label>
                      <img style=" cursor :hand; width :20px; height :16px " onclick="showmailatt();" src="images/nodeopen.BMP" /><input id="attname" style ="width :80%" type="text" runat ="server"  />
                      </td>
            </tr>
            <tr>
                <td align ="left" >
                <label id="label7" >
                    郵件主題</label>
                     <input id="subject" style ="width :80%" type="text" runat ="server"  />
               
                </td>
            </tr>
            <tr>
                <td align ="left" >
                    郵件內容:</td>
            </tr>
            <tr>
                <td align ="left" style="height: 358px">
                    <FTB:FreeTextBox ID="mailbody" Width ="90%" runat="server">
                    </FTB:FreeTextBox>
                </td>
            </tr>
            <tr>
                <td align ="left">
                    <asp:Button ID="sendbt" runat="server" OnClick="sendbt_Click" Text="發送郵件" />
                    <asp:Button ID="savebt" runat="server" Text="保存郵件" />
                    <asp:Button ID="clearbt" runat="server" Text="清空內容" />
                    <asp:Button ID="cancelbt" OnClick ="cancelbt_click" runat="server" Text="取消" />
                    </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

后台:

Imports System
Imports System.Net
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web
Imports System.Web.UI
Imports jmail
Imports System.Net.Sockets
Imports FreeTextBoxControls
Imports FreeTextBoxControls.Design
Imports FreeTextBoxControls.ImageGallery
Imports data
Imports mailclass

Partial Class sendmail
    Inherits System.Web.UI.Page

    Dim mymailclass As mailclass = New mailclass      ''业务逻辑层.

    Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        sendtime.Value = Calendar1.SelectedDate
        Calendar1.Style("visibility") = "hidden"
    End Sub

    Protected Sub sendbt_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim mg As jmail.Message = New jmail.Message
        Dim address As String = ""
        With (mg)
            .From = "chuxue1342@avl.com.cn"
            .FromName = "陈胜军"         
            .Charset = "UTF-8"
            .ContentType = "multipart/html"
            .ISOEncodeHeaders = False
            .Encoding = "BASE64"
            .Priority = Convert.ToByte(graddl.SelectedValue)
            If subject.Value <> "" Then
                .Subject = subject.Value
            Else
                Response.Write("主題不能爲空")
                Exit Sub
            End If

            .Body = mailbody.Text

            '添加附件
            Dim atts As String = attname.Value
            If atts <> "" Then
          Dim attal() As String = atts.Split(";")
                For i As Integer = 0 To attal.Length - 1
                    .AddAttachment(attal(i).ToString, False, "附件" & CInt(i) + 1)
                Next
            End If
            .MailServerUserName = "chuxue"             ' 用户名
            .MailServerPassWord = "****"                    '密码

            '添加聯系人
            Dim sendaddresses As String = sendto.Value
            If cto.Value <> "" Then
                sendaddresses += ";" & cto.Value
            End If
            If sto.Value <> "" Then
                sendaddresses += ";" & sto.Value
            End If

            Dim sendaddress() As String = sendaddresses.Split(";")
            If sendaddress.Length > 0 Then
                For j As Integer = 0 To sendaddress.Length - 1
                    If sendaddress(j).IndexOf("@") > 0 Then      '如果包含"@"则添加到收件人,否则查询数据库
                        .AddRecipient(sendaddress(j).ToString)
                    Else
                        mymailclass = New mailclass
                        address = mymailclass.getaddress(sendaddress(j).ToString)   ''通过用户名或组名得到email地址
                        If address.ToString <> "" Then
                            Dim emailstring() As String = address.Split(";")
                            For f As Integer = 0 To emailstring.Length - 1
                                .AddRecipient(emailstring(f).ToString)
                            Next
                        End If
                        address = ""
                    End If
                Next
              
            Else
                Response.Write("至少选择一个收件人!!")
            End If
        End With
        Try
            If (mg.Send("smtp.avl.com.cn", False) = True) Then         '发邮件服务器
                Response.Write("发送成功")
            End If
        Catch ex As Exception
            Response.Write("連接網絡超時,請重試!或與技術人員聯系!!")
        End Try
     
    End Sub

End Class

收邮件:

       Dim popmail As jmail.POP3Class = New jmail.POP3Class()
        Try
            popmail.Connect("chuxue", "*****", "pop.qq.com", "110")                        '用户名密码服务器
            popmail.Logging = True
            If popmail.Count > 0 Then
                For i As Integer = 1 To popmail.Count
                    Dim ms As Message = popmail.Messages(i)
                    With ms
                        .Charset = "UTF-8"
                        .ContentType = "multipart/html"                                     '这个很重要.避免邮件内容乱码
                        .ContentTransferEncoding = "BASE64"
                        .Encoding = "BASE64"
                    End With

                    With Label3
                        .Text += "邮件数" & popmail.Count & "<br/>"
                        .Text += ms.From & "发件人" & ms.From & "<br/>"
                        .Text += "邮件主题" & ms.Subject & "<br/>"
                        .Text += ms.Body & "<br/>"
                        .Text += ms.Date & "<br/>"
                    End With
                    Dim atts As jmail.Attachments = ms.Attachments
                    If atts.Count > 0 Then                  
                        Label3.Text += "附件数量" & atts.Count & "<br/>"
                        Dim j As Integer
                        For j = 0 To atts.Count - 1
                            Label3.Text += atts.Item(j).Name
                        Next

                      '可以通过atts.item(j).savetofile("保存路径")   保存附件到服务器
                    End If
                Next
            Else
                Label3.Text = "没有新邮件"
            End If

        Catch ex As Exception
            Label3.Text = "系统错误" & ex.Message
        Finally
            popmail.Disconnect()
        End Try

选择地址薄:

这里我主要写js代码!需要注意一点:  <base  target ="_self" ></base>


<head runat="server">
    <title>添加郵箱地址</title>
<script language="javascript" type="text/javascript">


  function returnvalue(sendmail,ctomail,stomail)  '通过dialogArguments获取父页面的变量传值
  {
  var tb=window.dialogArguments ;
  tb.sendto=sendmail;
  tb.cto=ctomail;
  tb.sto=stomail;
  tb.getmailaddress();
  }

</script>
  <link href ="body.css" rel ="Stylesheet" type ="text/css" />
  <base  target ="_self" ></base>
</head>
                    

后台:

这里我主要写添加按钮的代码:

    Protected Sub addbt_ServerClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles addbt.ServerClick
        Dim sends As String = ""
        Dim ctos As String = ""
        Dim stos As String = ""
        Dim i As Integer
        If sendtolb.Items.Count > 0 Then
            For i = 0 To sendtolb.Items.Count - 1
                If sends <> "" Then
                    sends += ";"
                End If
                sends += sendtolb.Items(i).Text
            Next
        End If
        If ctolb.Items.Count > 0 Then
            For i = 0 To ctolb.Items.Count - 1
                If ctos <> "" Then
                    ctos += ";"
                End If
                ctos += ctolb.Items(i).Text
            Next
        End If
        If stolb.Items.Count > 0 Then
            For i = 0 To stolb.Items.Count - 1
                If stos <> "" Then
                    stos += ";"
                End If
                stos += stolb.Items(i).Text
            Next
        End If
        Page.ClientScript.RegisterStartupScript(GetType(String), "", "<script>javascript:returnvalue('" + sends + "','" + ctos + "','" + stos + "');window.close();</script>")         ''注册js代码运行
    End Sub

jmail收取未读邮件(带附件)

/** jmail 收取"未读邮件" **/ //测试邮箱地址为 recieveceshi@163.com   /* pop.DownloadUnreadMessages()是用来“收取未读邮件...
  • aAa9079
  • aAa9079
  • 2014年08月27日 18:09
  • 1429

Jmail组件实现电子邮件收发

利用VC++开发电子邮件成型组,可以使用windows自带的MAPI及xi
  • hunterlew
  • hunterlew
  • 2014年11月18日 09:02
  • 1217

jmail + c#实现邮件的接收

引言    在上一篇博客中和大家分享了关于邮件发送的学习,既然邮件实现了发送,那么我们仍然需要实现邮件接收的 功能,那么下面就和大家分享一下关于邮件发送的基本实现过程,当然我们在实...
  • u013045437
  • u013045437
  • 2015年11月17日 23:52
  • 1945

基于jmail控件实现邮件发送源码

  • 2015年11月15日 21:27
  • 1.48MB
  • 下载

Jmail邮件接受与发送源码

  • 2010年05月08日 17:12
  • 88KB
  • 下载

Jmail发送邮件系统源码

  • 2009年12月29日 15:39
  • 446KB
  • 下载

Jmail/asp.net定时发邮件内容图片+附件程序源码

  • 2014年07月29日 15:39
  • 1.35MB
  • 下载

邮件发送,jmail,mail,asp邮件发送源码!

  • 2009年09月08日 11:51
  • 4KB
  • 下载

非常好的Jmail接收邮件和发送邮件源码

  • 2009年08月02日 15:04
  • 126KB
  • 下载

如何使用Jmail收发邮件

转载自:http://blog.csdn.net/wpabbs/article/details/2458006 Jmail 接收邮件: import javax.mail.Passwo...
  • WillCold
  • WillCold
  • 2014年08月15日 11:00
  • 580
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Jmail收发邮件(完整源码)
举报原因:
原因补充:

(最多只允许输入30个字)