Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-01-14
5
Medium Priority
?
367 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 7

Expert Comment

by:pr0t0c0l12
ID: 38774319
--------------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 2000 total points
ID: 38774330
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
ID: 38776161
0
 
LVL 19

Expert Comment

by:Amandeep Singh Bhullar
ID: 39936304
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
This Micro Tutorial will demonstrate how to analyze your website's back links using a network graph powered by a fusion table.
This Micro Tutorial will demonstrate using Google Doc how to import live data to another spreadsheet in Google Spreadsheets using the IMPORTRANGE function.

715 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