Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-01-14
5
Medium Priority
?
371 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:
Meir Rivkin 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
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.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

972 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