2015-04-26 147 views
-3

該代碼發出的電子郵件和工作正常,但由於某種原因,它不會出去的重要性。我究竟做錯了什麼。VB設置郵件優先級高

Option Explicit On 
Imports System.IO 
Imports System.Net.Mail 
Module SendMail 
Sub SendMessage() 
     Dim olApp As Object 
     Dim olMail As Object 
     Dim olNs As Object 
     Dim Priority As MailPriority 
    olApp = CreateObject("Outlook.Application") 
    olNs = olApp.GetNamespace("MAPI") 
    olMail = olApp.CreateItem(0) 

    With olMail 
     olMail.To = "" '// Add recipient 
     olMail.Cc = "" 
     olMail.Bcc = "" 
     olMail.Subject = "New File " 
     olMail.HTMLBody = "Your File is Ready " & Format(Now, "Long Date") 
     olMail.Attachments.Add = " " '// Add attachments to the message. 
     olMail.Priority = MailPriority.High '// High importance 
     olMail.Send() 
    End With 
    olMail = Nothing 
    olApp = Nothing 
    olNs = Nothing 
End Sub 
End Module 
+0

「On Error Resume Next」? *真*?您設置優先級的代碼可能會引發異常,您不知道它。 –

+0

發件人**從不**設置高優先級。這是接收者的電子郵件程序來決定。該屬性存在於您自己的文件夾中設置消息的前景中。 –

回答

2

嘗試使用System.Net.Mail命名空間來代替發送。

例子:

Option Strict On 
Option Explicit On 
Option Infer Off 
Imports System.Net.Mail 
Public Class Form1 
    Function SendEmail(ByVal Recipients As List(Of String), _ 
         ByVal FromAddress As String, _ 
         ByVal Subject As String, _ 
         ByVal Body As String, _ 
         ByVal UserName As String, _ 
         ByVal Password As String, _ 
         Optional ByVal Server As String = "smtp.gmail.com", _ 
         Optional ByVal Port As Integer = 587, _ 
         Optional ByVal Attachments As List(Of String) = Nothing) As String 
     Dim Email As New MailMessage() 
     Try 
      Dim SMTPServer As New SmtpClient 
      For Each Attachment As String In Attachments 
       Email.Attachments.Add(New Attachment(Attachment)) 
      Next 
      Email.From = New MailAddress(FromAddress) 
      For Each Recipient As String In Recipients 
       Email.To.Add(Recipient) 
      Next 
      Email.Subject = Subject 
      Email.Body = Body 
      '---------------------------------- 
      Email.Priority = MailPriority.High 
      '---------------------------------- 
      SMTPServer.Host = Server 
      SMTPServer.Port = Port 
      SMTPServer.Credentials = New System.Net.NetworkCredential(UserName, Password) 
      SMTPServer.EnableSsl = True 
      SMTPServer.Send(Email) 
      Email.Dispose() 
      Return "Email to " & Recipients(0) & " from " & FromAddress & " was sent." 
     Catch ex As SmtpException 
      Email.Dispose() 
      Return "Sending Email Failed. Smtp Error." 
     Catch ex As ArgumentOutOfRangeException 
      Email.Dispose() 
      Return "Sending Email Failed. Check Port Number." 
     Catch Ex As InvalidOperationException 
      Email.Dispose() 
      Return "Sending Email Failed. Check Port Number." 
     End Try 
    End Function 
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
     Dim Recipients As New List(Of String) 
     Recipients.Add("SomeEmailAddress") 
     Dim FromEmailAddress As String = Recipients(0) 
     Dim Subject As String = "Test From VB." 
     Dim Body As String = "email body text, if you are reading this from your gmail account, the program worked." 
     Dim UserName As String = "GMAIL USERNAME WITHOUT (@GMAIL>COM)" 
     Dim Password As String = "Password" 
     Dim Port As Integer = 587 
     Dim Server As String = "smtp.gmail.com" 
     Dim Attachments As New List(Of String) 
     MsgBox(SendEmail(Recipients, FromEmailAddress, Subject, Body, UserName, Password, Server, Port, Attachments)) 
    End Sub 
End Class 
+0

我沒有使用表單發送電子郵件,我試圖從Fileystemwatcher自動化SendMessage,當FileCreated – 0m3r

+0

只需修改代碼,但您需要它來與您的應用程序一起工作。它不是專門用於表單的。 –