如何通过gmail在.NET中发送电子邮件 [英] How to send an email in. NET through gmail

查看:96
本文介绍了如何通过gmail在.NET中发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过Gmail在.NET中发送电子邮件



我尝试过:



使用(MailMessage mail = new MailMessage())

{

mail.From = new MailAddress(email@gmail.com);

mail.To.Add(somebody@domain.com);

mail.Subject =Hello World;

邮件。 Body =

Hello

;

mail.IsBodyHtml = true;

mail.Attachments.Add(new Attachment(C:\\\ \\ file.zip));



使用(SmtpClient smtp = new SmtpClient(smtp.gmail.com,587))

{

smtp.Credentials = new NetworkCredential(email@gmail.com,password);

smtp.EnableSsl = true;

smtp.Send(mail);

}

}

how to send an email in. NET through Gmail

What I have tried:

using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress("email@gmail.com");
mail.To.Add("somebody@domain.com");
mail.Subject = "Hello World";
mail.Body = "

Hello

";
mail.IsBodyHtml = true;
mail.Attachments.Add(new Attachment("C:\\file.zip"));

using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
{
smtp.Credentials = new NetworkCredential("email@gmail.com", "password");
smtp.EnableSsl = true;
smtp.Send(mail);
}
}

推荐答案

这是我学到的,



使用System;

使用System.Net;

使用System.Net.Mail;



命名空间SendMailViaGmail

{

  课程计划

   {

   static void Main(string [] args)

   {



    

      string SendersAddress =Sendersaddress@gmail.com;

      string ReceiversAddress =ReceiversAddress@yahoo.com;

      const string SendersPassword =密码;

   const string subject =Testing;

const string body =嗨这是你的朋友;



      尝试

      {

    

         smtpClient smtp {}

         smtp.gmail.com和端口号是587

        SmtpClient smtp =新的SmtpClient

        {

          主持人=smtp.gmail.com,

          端口= 587,

           EnableSsl = true,

           DeliveryMethod = SmtpDeliveryMethod.Network,

          证书    = new NetworkCredential(SendersAddress,SendersPassword),

          超时= 3000

        };
This is what I learned,

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

namespace SendMailViaGmail
{
   class Program
   {
   static void Main(string[] args)
   {

    
      string SendersAddress = "Sendersaddress@gmail.com";
      string ReceiversAddress = "ReceiversAddress@yahoo.com";
      const string SendersPassword = "Password";
   const string subject = "Testing";
const string body = "Hi This is your friend ";

      try
      {
    
         SmtpClient smtp within{}
         smtp.gmail.com and port number is 587
        SmtpClient smtp = new SmtpClient
        {
           Host = "smtp.gmail.com",
           Port = 587,
           EnableSsl = true,
           DeliveryMethod = SmtpDeliveryMethod.Network,
           Credentials    = new NetworkCredential(SendersAddress, SendersPassword),
           Timeout = 3000
        };


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;
using Public_Uber.Models;

namespace Public_Uber.Controllers
{
    public class SendMailerController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
   
        // GET: SendMailer
        [HttpPost]
        public ActionResult Index(Public_Uber.Models.MailModel objModelMail, HttpPostedFileBase fileUploader)
        {
            if (ModelState.IsValid)
            {
                string from = "sthehfessor95@gmail.com"; //example:- sourabh9303@gmail.com
                using (MailMessage mail = new MailMessage(from, objModelMail.To))
                {

                    ExpandedUserDTO ob = new ExpandedUserDTO();
                    objModelMail.Body= "Your Username is:"+ob.UserName + "& Password:" + ob.Password;
                    mail.Subject = objModelMail.Subject;
                    mail.Body = objModelMail.Body;
                    if (fileUploader != null)
                    {
                        string fileName = Path.GetFileName(fileUploader.FileName);
                        mail.Attachments.Add(new Attachment(fileUploader.InputStream, fileName));
                    }
                    mail.IsBodyHtml = false;

                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = "smtp.gmail.com";
                    smtp.EnableSsl = true;
                    NetworkCredential networkCredential = new NetworkCredential(from, "Sithembiso95@");
                    smtp.UseDefaultCredentials = true;
                    smtp.Credentials = networkCredential;
                    smtp.Port = 587;

                    smtp.Send(mail);
                    ViewBag.Message = "Sent";
                    TempData["Message"] = "<script>alert('Email Successfully Sent! ');</script>";

                    return View("Index", objModelMail);
                }
            }
            else
            {
                return View();
            }
        }
    }
}



这篇关于如何通过gmail在.NET中发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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