asp.net C#发送邮件时出现问题 [英] problem in sending mail with asp.net C#

查看:90
本文介绍了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");

推荐答案

Gmail:
外发邮件服务器(SMTP):smtp.gmail.com
Google Gmail SMTP服务器要求在端口465上进行加密连接(SSL).

HOTMAIL:
外发邮件服务器(SMTP):smtp.live.com
端口号:25
HOTMAIL SMTP服务器要求端口995甚至25上的加密连接(SSL).

YAHOO !!:
外发邮件服务器(SMTP):smtp.mail.yahoo.com
端口号:465

AOL/AIM:
外发邮件服务器(SMTP):smtp.aol.com
端口号:587

Gmail:
Outgoing Mail server (SMTP): smtp.gmail.com
The Google Gmail SMTP Server requires an encrypted connection (SSL) on port 465.

HOTMAIL:
Outgoing Mail server (SMTP): smtp.live.com
Port number: 25
The HOTMAIL SMTP Server requires an encrypted connection (SSL) on port 995 and even on 25.

YAHOO!!:
Outgoing Mail server (SMTP): smtp.mail.yahoo.com
Port number: 465

AOL/AIM:
Outgoing Mail server (SMTP): smtp.aol.com
Port number: 587

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

    // Your gmail email address
    oMail.From = "gmailid@gmail.com";
    
    // Set recipient email address
    oMail.To = "someone@somedomain.com";
    
    // 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 "gmailid@gmail.com", then the user should be the same
    oServer.User = "gmailid@gmail.com";
    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);
    }
}


我认为原因是您的本地主机将默认的smtp服务器设置为gmail. msg.From可以是具有伪域名的伪邮件.我认为,无论来自谁,localhost smtp服务器都将发送它.
I think the reason is that your localhost has the default smtp server set as gmail. msg.From can be a fake mail with a fake domain. The localhost smtp server will send it no matter from whom it is, I think.


尝试下面的链接:
使用ASP.NET和C#发送邮件/联系表 [ ^ ]
使用smtp gmail服务器在asp.net中发送电子邮件 [ ^ ]
发送邮件asp.net [在asp.net中发送电子邮件 [此处 [
Try the links below:
Send Mail / Contact Form using ASP.NET and C#[^]
sending email in asp.net using smtp gmail server[^]
sending mail asp.net[^]
sending email in asp.net[^]

And a lot more here[^].


--Amit


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

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