Solved

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

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help needed in sql query 4 25
Can I skip a node in XML? 9 29
sql server cross db update 2 20
SQL Server Compression Decision 5 37
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial will demonstrate the easy use of Gmail embedding images in your email so the recipient of your email can view them in context.
This Micro Tutorial demonstrates how to quickly find related content for YourTango's posts using MozBar Chrome extension.

679 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