SMTP is not sending emails in gmail business account - smtp

This code is working well in gmail personal account, but when I try to use gmail business account, it is not working and keeps giving an error. 5.5.1 Authentication Required.
void SendEmail()
{
DataTable data = GetData();
DataTable email_data = GetEmailData();
data.TableName = "Employee_Data";
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(data);
using (MemoryStream memoryStream = new MemoryStream())
{
wb.SaveAs(memoryStream);
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
String from = "seong#abcd.net";
for (int i = 0; i < email_data.Rows.Count; i++)
{
String to = email_data.Rows[i][0].ToString();
using (MailMessage mm = new MailMessage(from, to))
{
mm.Subject = "Employees Attachment";
mm.Body = "Employees Exported Attachment";
mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "Employees.xlsx"));
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
credentials.UserName = "seong#abcd.net";
credentials.Password = "1234";
smtp.UseDefaultCredentials = true;
smtp.Credentials = credentials;
smtp.Port = 587;
smtp.Send(mm);
}
}
}
}
}

I solved this problem.
The account should not use 2nd verification in gmail if you want to use SMTP.
https://support.google.com/accounts/answer/1064203?hl=en&ref_topic=7189195
I can't control those things, I ask the administrator allow not to use 2nd verification.
So I can work with that account using SMTP.

Smtp method for Business Gsuite
require_once('class.phpmailer.php');
$mail = new PHPMailer(); // defaults to using php "mail()"
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Port = 587;
$mail->Host = "smtp.gmail.com";
$mail->Username = "Enter the user ID"; // SMTP account username
// SMTP account password
$mail->Password = "Enter your password";
$mail->SetFrom('Enter the User ID', 'Subject');
$mail->AddReplyTo("Enter the User ID", "Subject");
$mail->AddAddress($to, "Name");
$mail->Subject = "Contact Enquiry";
$message = '';
$mail->MsgHTML($message);
if($mail->Send()){
$mail->ClearAddresses();
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
} else {
echo "Mailer Error: " . $mail->ErrorInfo;
}

Related

Contact form with PHPMailser doesn't work

I would use PHPMailer with a contact form but I am doing something wrong because it doesn't work.
<?php
require ("class.phpmailer.php");
if (isset($_POST['submit'])) {
$name=$_POST['name'];
$subject=$_POST['subject'];
$email=$_POST['email'];
$message=$_POST['message'];
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.mail.com";
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Username = 'myemail#mail.com';
$mail-> Password = 12345;
$mail->From = ($email);
$mail->FromName = ($name);
$mail->addAddress = 'myemail#mail.com';
$mail->isHTML(false);
$mail->Subject = "Enquiry from Website submitted by $name";
}
if (!$mail->Send()) {
echo "<script>alert('Submission failed.');</script>";``
}
else {
echo "<script>alert('Email has been sent successfully.');</script>";
}
?>
Would you be so kind to give me a little big help? I don't understand where I'm doing wrong.
Thank you.
If you inspect debug output you can find a hint. Most possible reasons are incorrect parameters or maybe smtp server rejects request.
Please post debug output.

how to send mail from group email in script task ssis

I want to send email from SSIS. To do this I am using script task and inside that, i am using below code and same is working very well.
Now my manager told me to send mail from team_dev#uhg.com instead of hieko#uhg.com.I this case what would be my password as team_dev#uhg.com is group email.
email.From = new MailAddress("hieko#uhg.com");
email.To.Add("team_testing#uhg.com");
email.Subject = "Test Mail";
email.Body = "This Email is coming from SSIS Script Task";
try
{
MailMessage email = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("alaska");
// START
email.From = new MailAddress(SendMailFrom);
email.To.Add(SendMailTo);
email.Subject = SendMailSubject;
email.Body = SendMailBody;
//END
SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential(SendMailFrom, "my password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(email);
MessageBox.Show("Email was Successfully Sent ");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
Dts.TaskResult = (int)ScriptResults.Success;

Phpmailer can't send email (Could not instantiate mail function.;)

My sendmail function:
$usr = $user;
$body_msg = $this->getBodyEmail($fields);
$email_from = 'contato#mydomain.com';
$email_name = 'Name';
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SetLanguage("br", "phpmailer/language/phpmailer.lang-br.php");
$mail->setFrom( $email_from, $email_name);
$mail->isHTML(true);
$mail->Subject = 'Contato';
$mail->Body = $body_msg;
$mail->addAddress($fields['email'], $fields['nome']);
Of course I hid the domain.
So I try the $mail->send(), but it will not work and the $mail->ErrorInfo; displays: "Could not instantiate mail function.".
This system used to work without SMTP, I do not need it.
EDIT:
I changed the SetFrom to From and FromName and it seems to have worked.
$mail->From = 'contato#mydomain.com';
$mail->FromName = 'Name';
What it can be?
I changed the SetFrom to From and FromName and it seems to have worked.
$mail->From = 'contato#mydomain.com';
$mail->FromName = 'Name';

ASP.Net 2013 MVC 5 insert record and sends email with link to the record just created

I have setup an asp.net MVC project that inserts a record and then grabs the url information to be sent via email of which is a link in the body of the email. I would like to be able to pass the location of the details page with the id of the record if that makes sense? I am just not sure how to capture the full path and the id that I just created in the insert of the record just submitted. Can any one please help me on this. It would be greatly appreciated. This is my code and I need the url and id to be in the body of the email upon submit:
[HttpPost]
public ActionResult NewHire([Bind(Include = "ID,Manager,HR_Emp,Emp_FirstName,Emp_LastName,Emp_StartDate,Emp_OfficeLocation,Emp_Department,Emp_Title")] NewHire newhire)
{
if (ModelState.IsValid)
{
_entities.NewHires.Add(newhire);
_entities.SaveChanges();
MailMessage mail = new MailMessage();
mail.To.Add("Stephen.Michaels#brixmor.com");
mail.From = new MailAddress("someone#somewhere.com");
mail.Subject = "Test";
string Body = "<a href=http://www.google.com>" + "Click for Record" + "</a>";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "test";
smtp.Port = 25;
smtp.Send(mail);
return RedirectToAction("NewHire");
}
return View(newhire);
}
I would suggest you to use the Url.Action
if (ModelState.IsValid)
{
_entities.NewHires.Add(newhire);
_entities.SaveChanges();
string url = Url.Action("ActionName", "ControllerName", newhire.id);
MailMessage mail = new MailMessage();
mail.To.Add("Stephen.Michaels#brixmor.com");
mail.From = new MailAddress("someone#somewhere.com");
mail.Subject = "Test";
string Body = "<a href='"+ url +"'>" + "Click for Record" + "</a>";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "test";
smtp.Port = 25;
smtp.Send(mail);
return RedirectToAction("NewHire");
}

phpmailer does not connect to SMTP servers

i've been trying to use phpmailer and tried using live.com and gmail.com but it always can't connect to SMTP server. here's the full code (i've tried live.com using smtp.live.com but i get the same problem "Message could not be sent.Mailer Error: SMTP connect() failed.")
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'my emil address'; // SMTP username
$mail->Password = 'my password'; // SMTP password
$mail->SMTPSecure = 'TLS'; // Enable encryption, 'ssl' also accepted
$mail->From = 'the same email address';
$mail->FromName = 'Mailer';
//$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('another email address'); // Name is optional
//$mail->addReplyTo('info#example.com', 'Information');
//$mail->addCC('cc#example.com');
//$mail->addBCC('bcc#example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
I suspect it's because you've not set the port, and SMTPSecure should be lower case. Change this:
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
Beyond that, check that you're allowed to send outbound mail by your ISP/firewall and that your DNS is working.
Try This:
$mail->SMTPSecure = 'tls';
$mail->Host = 'tls://smtp.gmail.com';
$mail->Port = 587; //You have to define the Port
$mail->SMTPDebug = 3;
Remove This:
$mail->Host = 'smtp.gmail.com';
$mail->SMTPSecure = 'TLS';