-1

The official Gmail API documentation is horrendous. Not getting any clue to integrate Gmail API using .NET framework in vs2017. I wanted to send the input data of the Web form to a user's email.

asdfg
  • 1
  • 1
  • Possible duplicate of [Sending email in .NET through Gmail](https://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail) – Nomi Ali Jan 06 '19 at 06:45
  • I already sent using smtp but now i want to integrate with Gmail api and send it . I couldn't understand the .net documentation of the Gmail api. – asdfg Jan 06 '19 at 06:50
  • this link will be helpful, it really easy to implement https://developers.google.com/gmail/api/quickstart/dotnet – Nomi Ali Jan 06 '19 at 06:52
  • already tired but it's not working idk why – asdfg Jan 06 '19 at 06:55
  • Please show what you have tried and where did you get stuck. – Sir Rufo Jan 06 '19 at 07:20

1 Answers1

0

It would be a three step process -

  1. Define an HTML template which which describes how your mail should be presented.

  2. Write a small c# code to replace all place holders like your form fields , user name, etc.

     private string createEmailBody(string userName, string title, string message)  
    
    {  
    
    string body = string.Empty;  
    //using streamreader for reading my htmltemplate   
    
    using(StreamReader reader = new StreamReader(Server.MapPath("~/HtmlTemplate.html")))  
    
    {  
    
        body = reader.ReadToEnd();  
    
    }  
    
    body = body.Replace("{UserName}", userName); //replacing the required things  
    
    body = body.Replace("{Title}", title);  
    
    body = body.Replace("{message}", message);  
    //// Instead of message add you own parameters.
    
    return body;  
    

    }

  3. When form is submitted, call step 2 code first. Then use it's output to set mail body.

Code would be something like -

string smtpAddress = "smtp.gmail.com";
int portNumber = 587;
bool enableSSL = true;

/// This mail from can just be a display only mail I'd 
string emailFrom = "no-reply@gmail.com";

string subject = "your subject";
string body = createEmailBody();

using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
/// Add more to IDs if you want to send it to multiple people.

mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
// This is required to keep formatting of your html contemts

/// Add attachments if you want, this is optional
mail.Attachments.Add(new Attachment(yourfilepath));


   using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
    {
    smtp.Credentials = new NetworkCredential(your-smtp-account-email, your-smtp-account-password);
    smtp.EnableSsl = enableSSL;
    smtp.Send(mail);
    }
}

Refer this link for working example

https://www.c-sharpcorner.com/UploadFile/33b051/sending-mail-with-html-template/

EDIT: For using GMail API Using GMAIL APIs you will need two nuget packages: 1. Install-Package Google.Apis.Gmail.v1 2. Install-Package AE.Net.Mail

Code is very similar to what we have for normal SMTP mail send. It is explained at: http://jason.pettys.name/2014/10/27/sending-email-with-the-gmail-api-in-net-c/

Manoj Choudhari
  • 5,277
  • 2
  • 26
  • 37
  • thankx. i used this already when sending by SMTP but now i need to integrate with Gmail API. any code for that? – asdfg Jan 06 '19 at 09:06
  • check if [this link](http://jason.pettys.name/2014/10/27/sending-email-with-the-gmail-api-in-net-c/) helps. – Manoj Choudhari Jan 06 '19 at 09:23