使用smtp gmail服务器在asp.net中发送电子邮件 [英] sending email in asp.net using smtp gmail server

查看:57
本文介绍了使用smtp gmail服务器在asp.net中发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习使用gmail smtp服务器在asp.net中发送电子邮件..但是有未处理的异常。



i am just learning to send email in asp.net using gmail smtp server.. but having unhandled exception.

Unable to connect to the SErver.







这里的代码是什么问题






here is the code whats the problem

private void button1_Click(object sender, EventArgs e)
        {
            MailMessage mail = new MailMessage();
            mail.To.Add(textBox1.Text);
            
            mail.From = new MailAddress("farooqsp@gmail.com");
            mail.Subject = "Email using Gmail";

            string Body = "Hi, this mail is to test sending mail" +
                          "using Gmail in ASP.NET";
            mail.Body = Body;

            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com"; 
            smtp.Credentials = new System.Net.NetworkCredential
                 ("farooqsp@gmail.com", "helloworld");
            
            smtp.EnableSsl = true;
            smtp.Send(mail);

        }
    }

推荐答案

尝试设置smtp.Port = 587;在你打电话给smtp.Send(邮件)之前;



也可以在这里阅读 https://mail.google.com/support/bin/answer.py?answer=13287
try setting smtp.Port = 587; before you call smtp.Send(mail);

also have a read here https://mail.google.com/support/bin/answer.py?answer=13287


尝试此方法

try this method
public static Boolean SendingMail(string From, string To, string Subject, string Body)
    {
        
            try
            {
                MailMessage m = new MailMessage("Uma<test@gmail.com>", To);
                m.Subject = Subject;
                m.Body = Body;
                m.IsBodyHtml = true;
                m.From = new MailAddress(From);

                m.To.Add(new MailAddress(To));
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
              
                NetworkCredential authinfo = new NetworkCredential("test@gmail.com","password");
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = authinfo;
                smtp.Send(m);
                return true;

               


            }
            catch (Exception ex)
            {
                return false;
            }
        }


您可以使用给定的功能使用Gmail服务器发送邮件



You can use given function to send mail using Gmail server

public string SendMail(string toList, string from, string ccList, string subject, string body)
{

    MailMessage message = new MailMessage();
    SmtpClient smtpClient = new SmtpClient();
    string msg = string.Empty;
    try
    {
        MailAddress fromAddress = new MailAddress(from);
        message.From = fromAddress;
        message.To.Add(toList);
        if (ccList != null && ccList != string.Empty)
            message.CC.Add(ccList);
        message.Subject = subject;
        message.IsBodyHtml = true;
        message.Body = body;
        smtpClient.Host = "smtp.gmail.com";   // We use gmail as our smtp client
        smtpClient.Port = 587;
        smtpClient.EnableSsl = true;
        smtpClient.UseDefaultCredentials = true;
        smtpClient.Credentials = new System.Net.NetworkCredential("Your Gmail User Name", "Your Gmail Password");

        smtpClient.Send(message);
        msg = "Successful<BR>";
    }
    catch (Exception ex)
    {
        msg = ex.Message;
    }
    return msg;
}



参考链接: - 使用Gmail SMTP服务器发送电子邮件 [ ^ ]

或看看其中 [ ^ ]。

CP搜索 [ ^ ]解决完成发送邮件任务的许多问题。


Reference Link :- Sending Email using Gmail SMTP server[^]
or have a look there[^].
and the CP search[^] many question with solution to accomplishment of sending mail task.


这篇关于使用smtp gmail服务器在asp.net中发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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