2015-04-26 101 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

只需修改代码,但您需要它来与您的应用程序一起工作。它不是专门用于表单的。 –