如何通过asp.net web api发送电子邮件

时间:2013-08-22 16:33:02

标签: asp.net web-services wcf asp.net-web-api smtp

我正在制作一个Windows手机应用程序,我想在其中提供忘记密码功能。 当他按下忘记密码按钮时,应用程序将向用户存储的电子邮件ID发送电子邮件。 由于Windows手机中没有可用的smtp类,所以我想制作一个asp.net web api,它将从应用程序接收电子邮件ID(和密码)并将向该ID发送一封电子邮件。 我是Web服务的新手,对此知之甚少 请指导我如何完成这项任务,如果有人能提供代码,必须有一些像这样的网络服务。

4 个答案:

答案 0 :(得分:3)

以下是发送可以使用的电子邮件的功能示例。此外,下面有几个链接可以指导您在ASP.NET中创建Web服务

实际上,您不需要为此创建Web服务(尽管建议使用)。您可以创建一个快速而脏的Web表单,您可以在其中传递参数,例如example.com/sendemail.aspx?account=jack.smith&id=223232343

private static void SendEmail(string from, string from_name, string to, string cc, string bcc, string subject, string body, bool isHtml)
{
        SmtpClient mailClient = new SmtpClient(Config.SmptSettings.Server);
        mailClient.Credentials = new NetworkCredential(Config.SmptSettings.UserName, Config.SmptSettings.Password);
    mailClient.Port = Config.SmptSettings.Port;

    MailMessage message = new MailMessage();
    if (!string.IsNullOrEmpty(from_name))
    {
        message.From = new MailAddress(from, from_name);
    }
    else
    {
        message.From = new MailAddress(Formatter.UnFormatSqlInput(from));
    }

    message.To.Add(new MailAddress(to));

    if (!string.IsNullOrEmpty(cc))
    {
        message.CC.Add(cc);
    }

    if (!string.IsNullOrEmpty(bcc))
    {
        message.Bcc.Add(bcc);
    }

    message.Subject = subject;
    message.Body = body;
    message.IsBodyHtml = isHtml;

    mailClient.EnableSsl = Config.SmptSettings.SSL;
    mailClient.Send(message); 
}

答案 1 :(得分:1)

 ## Call this function in your WebApi controller ##

    private void sendEmailViaWebApi()
    {
                string subject = "Email Subject";
                string body = "Email body";
                string FromMail = "shahid@reckonbits.com.pk";
            string emailTo = "reciever@reckonbits.com.pk";
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("mail.reckonbits.com.pk");

                mail.From = new MailAddress(FromMail);
                mail.To.Add(emailTo);
                mail.Subject = subject;
                mail.Body = body;

                SmtpServer.Port = 25; 
                SmtpServer.Credentials = new System.Net.NetworkCredential("shahid@reckonbits.com.pk", "your password");
                SmtpServer.EnableSsl = false;
                SmtpServer.Send(mail);
    }

答案 2 :(得分:0)

对于使用.NET Core的用户,请注意目前不支持发送电子邮件。

通过GitHub查看.NET Core的以下问题: https://github.com/dotnet/corefx/issues/1006

已实施替代解决方案 - MailKit: https://github.com/jstedfast/MailKit

答案 3 :(得分:-5)

string random;
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnSubmit_Click(object sender, EventArgs e)
{
    string s1 = string.Empty;
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString);
    con.Open();
    SqlCommand cmd = new SqlCommand("select EmailId from tblEmail where EmailId='" + txtemail.Text + "'", con);
    SqlDataReader dr =cmd.ExecuteReader();

    if (dr.HasRows)
    {
        while (dr.Read())
        {
            s1 = dr.GetString(0);
        }
    }
    dr.Close();

    if (s1.Equals(txtemail.Text))
    {
        Session["Email"] = txtemail.Text;

        try
        {
            Random rndm = new Random();
            random = rndm.Next(99999).ToString();
            MailMessage message = new MailMessage();
            message.From = new MailAddress("yourid@gmail.com");
            message.To.Add(new MailAddress(txtemail.Text));
            message.Subject = " Hello... This is Your Serial No to change your password:";
            message.Body = random.ToString();
            message.IsBodyHtml = true;

            SmtpClient client = new SmtpClient();
            client.Host = "smtp.gmail.com";
            client.Port = 587;//Gmail port number
            client.UseDefaultCredentials = true;
            client.EnableSsl = true;
            client.Credentials = new System.Net.NetworkCredential("yourid@gmail.com", "yourpassword");
            client.Send(message);
            SqlCommand cmd1 = new SqlCommand("insert into tblRandom values('" + txtemail.Text + "','" + random.ToString() + "')", con);

            cmd1.ExecuteNonQuery();
            con.Close();
            Response.Write("<script>alert('Security Code Successfully Sent in Your Email Id')</script>");

            Response.Redirect("~/anotherpage.aspx");
        }
        catch (Exception)
        {
           // Response.Write(ee.Message);
            lblmsg.Text = "Please Enter Email-Id..";
            lblmsg.Visible = true;
            //MessageBox.Show("Please Enter Email-ID");
            //Response.Write("<script>alert('Please Enter Email-ID')</script>");
        }
    }
    else
    {
        lblmsg.Text = "Please Enter Correct Email-Id..";
        lblmsg.Visible = true;
    }
}