本文介绍了asp.net C#发送邮件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在utube上观看了用于发送邮件的视频,但是此代码仅从gmail发送邮件
我想从任何不是Gmail的邮件中发送邮件
我该怎么办?

这是我的代码

i watch a video on utube for sending mail but this code send a mail from gmail only
i want send from any mail not gmail only
what can i do ?

this is my code

MailMessage msg = new MailMessage();
            msg.From = new MailAddress(TextBox1.Text);
            msg.To.Add(TextBox3.Text);
            msg.Subject = TextBox4.Text;
            msg.Body = TextBox5.Text;
            SmtpClient client = new SmtpClient();
            client.Host = "localhost";
            client.Port = 25;
            client.Credentials = new NetworkCredential(TextBox1.Text, TextBox2.Text);
            client.EnableSsl = true;
            client.Send(msg);
            Response.Write("Mail Sent");

推荐答案

private void SendMail(string smtpserver="smtp.gmail.com")
{
    SmtpMail oMail = new SmtpMail("TryIt");
    SmtpClient oSmtp = new SmtpClient();

    // Your gmail email address
    oMail.From = "[email protected]";

    // Set recipient email address
    oMail.To = "[email protected]";

    // Set email subject
    oMail.Subject = "test email from gmail account";

    // Set email body
    oMail.TextBody = "this is a test email sent from c# project with gmail.";

    // Gmail SMTP server address
    SmtpServer oServer = new SmtpServer(smtpserver);

    // If you want to use direct SSL 465 port, 
    // please add this line, otherwise TLS will be used.
    // oServer.Port = 465;

    // detect SSL/TLS automatically
    oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

    // Gmail user authentication
    // For example: your email is "[email protected]", then the user should be the same
    oServer.User = "[email protected]";
    oServer.Password = "yourpassword";

    try
    {
        Console.WriteLine("start to send email over SSL ...");
        oSmtp.SendMail(oServer, oMail);
        Console.WriteLine("email was sent successfully!");
    }
    catch (Exception ep)
    {
        Console.WriteLine("failed to send email with the following error:");
        Console.WriteLine(ep.Message);
    }
}




这篇关于asp.net C#发送邮件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 11:18