开发者

Send EMail with Visual Studio 2008 using C#

I am using Visual Studio 2008 Express with C#. I have been trying to get an email routine to work in the code-behind on an aspx page.

All the MSDN examples, even those stated to be for .Net 3.5, do not compile. The MailMessage class has apparently changed a few times. Here is code that does compile, but this line of code, SmtpMail.Send(msg), has an error message that is vague:

"The best overloaded method match for 'System.Net.Mail.SmtpClient.Send(System.Net.Mail.MailMessage)' has some invalid arguements.

Can anyone see what the invalid arguements could be? This is all that is preventing this from working.

using System.Net;
using System.Net.Mail;

MailMessage msg = new MailMessage();

msg.ToAddress = new MailAddress("someone@yourCompany.com");
msg.FromAddress = ("me@myCompany.com");
msg.CCAddress = ("boss@myCompany.com");

msg.EmailMessage = "Order message te开发者_运维问答st";
msg.EmailSubject = "Order Confirmation";
msg.MailEncoding = "html";
msg.MailPriority = MailPriority.Normal.ToString();

SmtpClient SmtpMail = new SmtpClient();

SmtpMail.Host = "smtpout.secureserver.net";
SmtpMail.Port = 25;

try
{
    SmtpMail.Send(msg);    // This is where the error occurs.
}
catch (Exception ex)
{
    //  Incomplete here
}


The example below uses C# .net 3.0 framework.

Here is the extra libraries I use:

        using System.Net.Mail;  //.MailMessage
        using Microsoft.VisualBasic; //imports control characters for string formatting

Here is my function for emailing:

        String strPassed = ""; 
        System.Web.Mail.MailMessage
        String strEmailTo = "touser@xyz.org";
        String strEmailFrom = "fromuser@xyz.org";
        String strEmailBCC = "";
        String strEmailSubject;
        String strEmailBody = "";

        strEmailSubject = "Attention User - Request Email";
        strEmailBody = "You requested your password for xyz site. <br><br>" +
            " <b> Account Information </b> ...<br> ";
        strEmailBody = strMessage;
        strEmailTo = strEmail;

        System.Net.Mail.MailMessage objMessage = new System.Net.Mail.MailMessage(strEmailFrom, strEmailTo);
        objMessage.IsBodyHtml = true;
        objMessage.Subject = strEmailSubject;
        objMessage.Body = strEmailBody;  //emailbody;

        MailAddressCollection objAddress = new MailAddressCollection();
        System.Net.Mail.SmtpClient objClientPath = new System.Net.Mail.SmtpClient();
        SmtpClient smtp = new SmtpClient();
        try
        {
            strPassed = "passed:emailed";
            objClientPath.Host = "mailhost.xyz.org";
            objClientPath.Send(objMessage); 
        }
        catch (System.Exception err)
        {
            strPassed = "failed:"+ err.ToString();//''if an error 
        }
        return strPassed;


It looks like you're trying to do something quite complicated; I would try with an even simpler example first and work your way up. The website SystemNetMail.com has a lot of resources you might find useful. This is their simple example:

//create the mail message
MailMessage mail = new MailMessage();

//set the addresses
mail.From = new MailAddress("me@mycompany.com");
mail.To.Add("you@yourcompany.com");

//set the content
mail.Subject = "This is an email";
mail.Body = "this is a sample body";

//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1");
smtp.Send(mail);


It looks like you're using a custom wrapper for MailMessage. None of those properties are members of the .NET classes (for any version I can find).

For 3.5, you'd use:

msg.To.Add(new MailAddress("someone@yourCompany.com"));
msg.From.Add(new MailAddress("me@myCompany.com"));
msg.CC.Add(new MailAddress("boss@myCompany.com"));

msg.Body = "Order message test";
msg.Subject = "Order Confirmation";
msg.BodyEncoding = Encoding.Default;
msg.Priority = MailPriority.Normal;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜