Private Sub cmdAttachment_Click() On Error Resume Next
With CommonDialog1 .DialogTitle = "Insert Attachment" .Filter = "All Files (*.*)|*.*" .ShowOpen
If Dir(.FileName) <> "" Then txtAttachment.Text = .FileName
' 顯示 Attachment 的 Icon oleAttachment.SourceDoc = .FileName oleAttachment.CreateEmbed .FileName Else MsgBox "Attachment is not available.", vbCritical End If End With End SubPrivate Sub cmdSend_Click() ' 開始 MAPI Session MAPISession1.SignOn
' 當 MAPI Session 建立後, ' Session 所產生的 Handle 會存於 SessionID 屬性中 If MAPISession1.SessionID <> 0 Then
' 設定 Attachment (附件) If Dir(txtAttachment.Text) <> "" Then MAPIMessages1.MsgNoteText = MAPIMessages1.MsgNoteText & vbCrLf MAPIMessages1.AttachmentPosition = Len(MAPIMessages1.MsgNoteText) - 1 MAPIMessages1.AttachmentPathName = txtAttachment.Text End If
' 傳送 E-Mail 且不顯示"郵件對話盒" (直接傳送) MAPIMessages1.Send False End If
' 結束 MAPI Session MAPISession1.SignOff End SubPrivate Sub cmdExit_Click() End End Sub Mapi需要這個
Dim OutLooks As New Outlook.ApplicationPrivate Sub Command1_Click() Call send_mail("郵件的標題") End SubPublic Function OutLookMailto(OutLooks As Outlook.Application, _ ByVal strSubject As String, _ ByVal strText As String, colAddrList As Collection, _ colAttachments As Collection) As Boolean Dim Mail As MailItem Dim strTemp Set Mail = OutLooks.CreateItem(olMailItem) '設定要一個新的Mail Item With Mail For Each strTemp In colAddrList .Recipients.Add strTemp '新增收件人 Next .Subject = strSubject '主旨 .Body = strText '內容 .Save '存入寄件夾 .Send '出信件 End With Set Mail = Nothing OutLookMailto = True Exit Function Errh: OutLookMailto = False End FunctionPrivate Sub send_mail(ByVal strSubject As String) Dim colAddrs As New Collection Dim colAttachs As New Collection Dim strBody As String Dim strText As String Dim blnSendOK As Boolean Dim SQL As String colAddrs.Add "你的郵箱" colAttachs.Add "" strText = " 郵件內容 " blnSendOK = OutLookMailto(OutLooks, strSubject, strText, colAddrs, colAttachs) If blnSendOK = True Then MsgBox "郵件發送成功!", vbInformation Else MsgBox "郵件發送未成功!", vbInformation End If 'End End Sub
http://www.sijiqing.com/vbgood/code/index.asp?action=read&id=1072
'参数:MCtro----控件名;StrDZ--邮件地址;StrUserName--用户名;StrPWD--密码;StrSubject-主题;StrText--内容
'调用方法: call SubSendMail(传递对应的参数)
Private Sub SubSendMail(MCtro As MAPISession, StrDZ As String, StrUserName As String, StrPWD As String, StrSubject As String, StrText As String)
MAPISession1.UserName = StrUserName
MAPISession1.Password = StrPWD
MAPISession1.SignOn
MAPIMessages1.SessionID = MAPISession1.SessionID
MAPIMessages1.Compose
MAPIMessages1.RecipAddress = StrDZ '注意这里就是了
MAPIMessages1.MsgSubject = StrSubject
MAPIMessages1.MsgNoteText = StrText
MAPIMessages1.ResolveName
MAPIMessages1.Send
MAPISession1.SignOff
End Sub
而且支持用户密码验证的
支持附件
就是不告诉你
注:不是我做的,没代码,只知道如何调用!
On Error Resume Next
With CommonDialog1
.DialogTitle = "Insert Attachment"
.Filter = "All Files (*.*)|*.*"
.ShowOpen
If Dir(.FileName) <> "" Then
txtAttachment.Text = .FileName
' 顯示 Attachment 的 Icon
oleAttachment.SourceDoc = .FileName
oleAttachment.CreateEmbed .FileName
Else
MsgBox "Attachment is not available.", vbCritical
End If
End With
End SubPrivate Sub cmdSend_Click()
' 開始 MAPI Session
MAPISession1.SignOn
' 當 MAPI Session 建立後,
' Session 所產生的 Handle 會存於 SessionID 屬性中
If MAPISession1.SessionID <> 0 Then
' 建立 MAPIMessages 與 MAPISession 間之有效關聯
MAPIMessages1.SessionID = MAPISession1.SessionID
' 建立新的 E-Mail Message
MAPIMessages1.Compose
' 收件者 (Recipient's Name)
MAPIMessages1.RecipDisplayName = txtName.Text
' 收件者的 E-Mail Address
MAPIMessages1.RecipAddress = "smtp: " & txtAddress.Text
' 是否要檢查收件者有無存在於全域 (Global) 或個人 (Personal) 通訊錄裏
MAPIMessages1.AddressResolveUI = True
' 檢查收件者有無存在於全域 (Global) 或個人 (Personal) 通訊錄裏
MAPIMessages1.ResolveName
' E-Mail 的主旨
MAPIMessages1.MsgSubject = txtSubject.Text
' E-Mail 的內文
MAPIMessages1.MsgNoteText = txtNote.Text
' 設定 Attachment (附件)
If Dir(txtAttachment.Text) <> "" Then
MAPIMessages1.MsgNoteText = MAPIMessages1.MsgNoteText & vbCrLf
MAPIMessages1.AttachmentPosition = Len(MAPIMessages1.MsgNoteText) - 1
MAPIMessages1.AttachmentPathName = txtAttachment.Text
End If
' 傳送 E-Mail 且不顯示"郵件對話盒" (直接傳送)
MAPIMessages1.Send False
End If
' 結束 MAPI Session
MAPISession1.SignOff
End SubPrivate Sub cmdExit_Click()
End
End Sub
Mapi需要這個
Call send_mail("郵件的標題")
End SubPublic Function OutLookMailto(OutLooks As Outlook.Application, _
ByVal strSubject As String, _
ByVal strText As String, colAddrList As Collection, _
colAttachments As Collection) As Boolean
Dim Mail As MailItem
Dim strTemp
Set Mail = OutLooks.CreateItem(olMailItem) '設定要一個新的Mail Item
With Mail
For Each strTemp In colAddrList
.Recipients.Add strTemp '新增收件人
Next
.Subject = strSubject '主旨
.Body = strText '內容
.Save '存入寄件夾
.Send '出信件
End With
Set Mail = Nothing
OutLookMailto = True
Exit Function
Errh:
OutLookMailto = False
End FunctionPrivate Sub send_mail(ByVal strSubject As String)
Dim colAddrs As New Collection
Dim colAttachs As New Collection
Dim strBody As String
Dim strText As String
Dim blnSendOK As Boolean
Dim SQL As String
colAddrs.Add "你的郵箱"
colAttachs.Add ""
strText = " 郵件內容 "
blnSendOK = OutLookMailto(OutLooks, strSubject, strText, colAddrs, colAttachs)
If blnSendOK = True Then
MsgBox "郵件發送成功!", vbInformation
Else
MsgBox "郵件發送未成功!", vbInformation
End If
'End
End Sub