convert vb email send code to c# [closed]

2019-09-22 02:42发布

问题:

currently i use this code to send an email through vb applications. I am now having to use c# and i was wondering if there was something similar i could write in c# which would do the same?

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:

You could use the SmtpClient class to send email in .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);
}


标签: c# email vba smtp