Solved

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

Posted on 2013-01-14
5
365 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 500 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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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…
This Micro Tutorial will demonstrate Google Calendar to monitor updates with top sites, such as Facebook, Google, Twitter, etc. with Marketing News. Each update of Google Calendar can be monitored, correlate dips and spikes in your website traffic, …
This Micro Tutorial will demonstrate common damaging and frequent mistakes I see in most analytic audits. Most of them are campaign tagging mistakes, so this video will break it down into simple steps.

630 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