2011-03-08 123 views

回答

4

的一般程序如下:

  • 創建mailto:鏈接字符串所需要的信息
  • 傳遞字符串Process.Start。這將打開默認郵件客戶端,不需要Outlook。

例如,字符串可能如下所示:mailto:[email protected]?subject=Hello&body=test。單個字段必須正確轉義(URL編碼)。有關該語法的更多信息,請參見RFC 2368

通過使用mailto字符串中的attachment參數可以添加附件。根據a comment on MSDN這必須加倍引用,但。那就是:

mailto:[email protected]?subject=Hello&body=Test&attachment=""C:\file.txt"" 
+0

好吧,我會盡量讓你知道。還有一件事。如果依戀不止一個,那麼如何實現? – Rupesh 2011-03-08 10:37:01

1

這個任務可以使用辦公室互操作來實現,只是我們projecto添加到`Microsoft.Office.Interop.Outlook」的參考。然後在你的applicattion可以創建一個郵件,然後附加文件如下:

Imports Microsoft.Office.Interop 

... 

Dim Outlook As Outlook.Application 
Dim Mail As Outlook.MailItem 
Dim Acc As Outlook.Account 

Outlook = New Outlook.Application() 
Mail = Outlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem) 
Mail.To = "[email protected]" 
Mail.Subject = "Hello World!" 

'If you have multiple accounts you could change it in sender: 
For Each Acc In Outlook.Session.Accounts 
    'Select first pop3 for instance. 
    If Acc.AccountType = Microsoft.Office.Interop.Outlook.OlAccountType.olPop3 Then 
     Mail.Sender = Acc 
    End If 
Next 

'Take default account if no sender... 
If Not Sender Is Nothing Then Mail.Sender = Sender.CurrentUser.AddressEntry 

'Attach files 
Mail.Attachments.Add("C:\Path\To\File.pdf") 
Mail.Attachments.Add("C:\Path\To\File1.pdf") 

'Append some text: 
Mail.HTMLBody &= "Hello World!" 

Mail.Display() 

這是一個老問題,但我想它可以幫助別人。

-1

找到了這篇文章。

Programmatically adding attachments to emails in C# and VB.NET

鏈接的文章文檔如何使用MAPI32.DLL提供的MAPISendMail則功能。

<DllImport("MAPI32.DLL")> _ 
    Private Shared Function MAPISendMail(ByVal sess As IntPtr, 
     ByVal hwnd As IntPtr, ByVal message As MapiMessage, 
     ByVal flg As Integer, ByVal rsv As Integer) As Integer 
    End Function 

    Private Function SendMail(ByVal strSubject As String, 
     ByVal strBody As String, ByVal how As Integer) As Integer 
     Dim msg As MapiMessage = New MapiMessage() 
     msg.subject = strSubject 
     msg.noteText = strBody 

     msg.recips = GetRecipients(msg.recipCount) 
     msg.files = GetAttachments(msg.fileCount) 

     m_lastError = MAPISendMail(New IntPtr(0), New IntPtr(0), msg, how, 
      0) 
     If m_lastError > 1 Then 
      MessageBox.Show("MAPISendMail failed! " + GetLastError(), 
       "MAPISendMail") 
     End If 

     Cleanup(msg) 
     Return m_lastError 
    End Function 

希望它能幫助那些可能像我一樣在這裏找到自己的方式。

0

如果有人想這樣做,而不使用Outlook ...

Imports System.Net.Mail 

Public Function SendEmail(EmailBody As String, EmailSubject As String, EmailTo As String, AttachmentPath As String, EmailAsHTML As Boolean) 

    Dim Mail As New MailMessage 

    Try 
     Dim SMTP As New SmtpClient("smtp.gmail.com") 
     SMTP.EnableSsl = True 
     SMTP.Credentials = New System.Net.NetworkCredential("[your gmail [email protected]]", "[the associated password]") 
     SMTP.Port = 587 

     Mail.From = New MailAddress("""[Friendly Name]"" <[your gmail [email protected]>") 

     'Split Multiple Addresses 
     If EmailTo.Contains(";") Then 
      For Each EmailAddress In EmailTo.Split(";") 
       Mail.To.Add(Trim(EmailAddress)) 
      Next 
     Else 
      Mail.To.Add(Trim(EmailTo)) 
     End If 

     Mail.Subject = EmailSubject 
     Mail.Body = EmailBody 
     If AttachmentPath <> "" Then Mail.Attachments.Add(New Mail.Attachment(AttachmentPath)) 
     Mail.IsBodyHtml = EmailAsHTML 

     SMTP.Send(Mail) 

     'Clear Mail Object 
     Mail.Dispose() 

     'Function Return 
     Return True 

    Catch ex As Exception 

     'Function Return 
     Return False 

    End Try 
End Function