转换vb电子邮件发送代码到c#

时间:2011-09-07 09:11:48

标签: c# email vba smtp

目前我使用此代码通过vb应用程序发送电子邮件。我现在不得不使用c#而且我想知道是否有类似的东西我可以用c#编写哪些会做同样的事情?

Set oEmail = CreateObject("CDO.Message")
    oEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")=2
    oEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "127.0.0.1"
    oEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") =25 
    oEmail.Configuration.Fields.Update
    oEmail.From = "test@test.co.uk"
    oEmail.To = "test@test.com"
    oEmail.Subject = "Subject!!"
    oEmail.Textbody = "The body"
    oEmail.Send
    Set oEmail = nothing

1 个答案:

答案 0 :(得分:1)

您可以使用SmtpClient类在.NET中发送电子邮件:

using (var client = new SmtpClient("127.0.0.1", 25))
using (var message = new MailMessage("from@foo.com", "to@bar.com"))
{
    message.Subject = "some subject";
    message.Body = "test body";
    client.Send(message);
}