Solved

how to send an email  from a Gmail account  using ASP.Net (C#)

Posted on 2013-01-14
5
357 Views
Last Modified: 2014-11-12
I'm a very beginner to use C# language  for developing web applications . And i want to send an email from my Gmail account to specific employees that i will choose  from  a dropdown list

I have a table in the database  called  
Employee and it has the following attributes
* employee ID
employee Name
DepID
Designation
UserId
EmpGender
ShiftID
EmployeeEmailAddress



i'm using MS visual studio 2010 and i inserted this code for sending Email


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;


public partial class HRemployee_Staff_Absent_Report : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnWarning_Click(object sender, EventArgs e)
    {
        string pGmailEmail = "candlfatma922@gmail.com"; //your gmail account
        string pGmailPassword = "Ff81219922"; //your password
        string pTo = "candl922@hotmail.com"; //To email address 
        string pSubject = "Absences Warning";
        string pBody = "Dear employee, your absences ";
        int employeeID = int.Parse(DDemployeeID.SelectedValue);


        System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();
        myMail.Fields.Add
        ("http://schemas.microsoft.com/cdo/configuration/smtpserver",
        "smtp.gmail.com");
        myMail.Fields.Add
        ("http://schemas.microsoft.com/cdo/configuration/smtpserverport",
        "465");
        myMail.Fields.Add
        ("http://schemas.microsoft.com/cdo/configuration/sendusing",
        "2");

        myMail.Fields.Add
         ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
        myMail.Fields.Add
        ("http://schemas.microsoft.com/cdo/configuration/sendusername",
        pGmailEmail);
        myMail.Fields.Add
        ("http://schemas.microsoft.com/cdo/configuration/sendpassword",
        pGmailPassword);
        myMail.Fields.Add
        ("http://schemas.microsoft.com/cdo/configuration/smtpusessl",
        "true");

        myMail.From = pGmailEmail;
        myMail.To = pTo;
        myMail.Subject = pSubject;
        myMail.BodyFormat = System.Web.Mail.MailFormat.Text;
        myMail.Body = pBody;

        System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465";
        System.Web.Mail.SmtpMail.Send(myMail);

        LblMessage.Text=" The Warning as been sent";
            

    }
}

Open in new window



How can i modify this code so i can send an email for specific  employee email from a dropdown list that displays the employee ID
0
Comment
Question by:Candel922
5 Comments
 
LVL 7

Expert Comment

by:pr0t0c0l12
Comment Utility
--------------configuration file --------------------------
<configuration>
  <system.net>
    <mailSettings>
      <smtp from="example@domain.com" deliveryMethod="Network">
          <network host="smtp.gmail.com" port="587"
              userName="example@domain.com" password="password"/>
      </smtp>
    </mailSettings>
  </system.net>
</configuration>
----------------------------------------


-----------------button function ---------------------

var message = new MailMessage("test.email.com");
// here is an important part:
message.From = new MailAddress("example@domain.com", "Mailer");
// it's superfluous part here since from address is defined in .config file
// in my example. But since you don't use .config file, you will need it.

var client = new SmtpClient();
client.EnableSsl = true;
client.Send(message);
------------------------------------------------------

hope that helps you.
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
Comment Utility
add the following function:

private void SendEmail(string from, List<string> toList, string subject, string body)
{

const string fromPassword = "fromPassword";

var smtp = new SmtpClient
           {
               Host = "smtp.gmail.com",
               Port = 587,
               EnableSsl = true,
               DeliveryMethod = SmtpDeliveryMethod.Network,
               UseDefaultCredentials = false,
               Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
           };
var message = new MailMessage
                     {
 From = new MailAddress(from),
                         Subject = subject,
                         Body = body
                     };

foreach(var toAddress in toList){
message.To.Add(toAddress);
}

{
    smtp.Send(message);
}
}

Open in new window

0
 
LVL 21

Expert Comment

by:masterpass
Comment Utility
0
 
LVL 19

Expert Comment

by:Amandeep Singh Bhullar
Comment Utility
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This Micro Tutorial will demonstrate without sampling how to find out top organic landing pages. The hack gets around the standard way to find the pages in Google Analytics results in sampling for larger sites.
This Micro Tutorial demonstrates how to quickly find related content for YourTango's posts using MozBar Chrome extension.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now