如何发送多部分/替代电子邮件? [英] How do I send multipart/alternative emails?

查看:144
本文介绍了如何发送多部分/替代电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,
我正在VB.net中进行开发并使用VS2008
问题
如何发送多部分/替代电子邮件?

可以通过Net.Mail完成吗?还是有替代软件包?

greetings,
i am developing in VB.net and using VS2008
Question
How do I send multipart/alternative emails?

can it be done via Net.Mail? or is there an alternative package?

推荐答案

将附件添加到邮件时,会自动创建多部分mime消息.

阅读以下链接:
http://www.systemnetmail.com/faq/3.1.3.aspx [ ^ ]

http://social.msdn.microsoft.com/Forums/zh/netfxnetcom/thread/660b744d-59c1-4d59-b513-cc57d1a7385e [ C#.NET中的POP3客户端 [ http://www.4-asp.net/articles/email3.aspx [ ^ ]
Multi part mime messages are automatically created when you add attachments to your mail.

Read the following links :
http://www.systemnetmail.com/faq/3.1.3.aspx[^]

http://social.msdn.microsoft.com/Forums/en/netfxnetcom/thread/660b744d-59c1-4d59-b513-cc57d1a7385e[^]

A POP3 Client in C# .NET[^]

http://www.4-asp.net/articles/email3.aspx[^]


好的,这样我做到了...它是这样的.
ok so i did it... it goes like this.
Sub SendEmail()
'here we need a couple of things 
'1)an instance of System.Net.SmtpClient
'2)an instance of Net.Mail.MailMessage

'part 1
'declare
Dim oSmptServer as New System.net.SmtpClient
    oSmtpServer.Host = "my_SmtpServer.net" 'your connection url
    oSmtpServer.Timeout = 60 'integer in seconds
    oSmtpServer.Port = 25 'the connection port is by default
    oSmtpServer.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network

'part 2
    Dim username as String = "my_username"
    Dim password as String = "my_password"
    'setup the Credentials 
    oSmtpServer.Credentials = New Net.NetworkCredential(username. password)
    
    Dim email_to as String = "recepient_email@somemail.com"
    Dim email_from as String = "my_email_address@myhost.com"
    Dim Subject as String = "The Subject..."
    Dim email_body as string = "This is text/plain content"
    Dim message as Net.Mail.MailMessage(email_from, email_to)

    'set it up
    message.Subject = Subject
    message.body = email_body
    'Now upto here we have only plain text for the Email Client to display
    'next we will add the alternative!(HTML)
    
    'need an AlternativeView object
    'set it up like this
    'the Mail.AlternateView has many overloads 
    'One can provide the path to a html file for the 1st constructor property
    'the second will let the email client know we are sending       
    'multipart/alternative
     
    Dim html as New Mail.AlternateView("<html><body>Any html tah can go_         here</body></html>" , "text/html")
     'now we have to add this property to our message
     message.AlternateViews.add(html)
     'set the Encoding
     message.Encoding = System.Text.Encoding.UTF8 'standard
     
     'finally send it out
     Dim user_state as String = " -Token"
     oSmtpServer.SendAsync(message,user_state)
End Sub



如果电子邮件客户端禁用了html,此方法将为您服务!
那么他们将不会看到标记,而是会看到您的
文本版本 您将提供给字符串的电子邮件,称为email_body

就是这样.
享受



This method will cover you in case the email client has html disabled!
then they will not see the markup instead they will see your text version of
the email which you would supply to the String called email_body

Thats all.
enjoy


这篇关于如何发送多部分/替代电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆