VB 将邮件优先级设置为高
VB Set Mail Priority High
代码发送了电子邮件并且工作正常,但由于某种原因,它的重要性不高。我做错了什么。
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
尝试使用 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
看一看 MSDN documentation for Outlook 2013's MailItem
class, it appears that there is no Priority
property (which you're trying to set). Try setting the Importance
property 来判断。
…或者如果您不需要 Outlook 功能,而只想发送邮件,只需使用 .NET 自己的 SmtpClient
class 来发送邮件。
代码发送了电子邮件并且工作正常,但由于某种原因,它的重要性不高。我做错了什么。
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
尝试使用 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
看一看 MSDN documentation for Outlook 2013's MailItem
class, it appears that there is no Priority
property (which you're trying to set). Try setting the Importance
property 来判断。
…或者如果您不需要 Outlook 功能,而只想发送邮件,只需使用 .NET 自己的 SmtpClient
class 来发送邮件。