how to send mail from group email in script task ssis - 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;

Related

StmpClient use works for remote users via VPN but not for users in office, on the network

I've implemented the .NET SmtpClient to send emails programmatically. Multiple users of the application and at various stages, an email is automatically sent. For users that are running the application from home, over VPN, the email almost always works. For users at the office, the email fails, "Failure sending mail" is exception message with no inner exception. The email server is MS Exchange, for large financial institution. Not using SSL, using port 25.
SmtpClient mailClient = new SmtpClient (getSmtpServer(), getSmtpPort());
mailClient.Timeout = 20000;
mailClient.UseDefaultCredentials = true;
MailAddress from = new MailAddress ( getSmtpFromAcct(),
"My Application",
System.Text.Encoding.UTF8);
MailAddress to = new MailAddress ( toEmail );
MailMessage message = new MailMessage(from, to);
for( int i = 1; i < toUsers.Count; i++)
{
BeanUser beanUser = toUsers[ i ];
message.To.Add( beanUser.getEmail() );
}
message.Subject = subject;
message.Body = messageBody;
mailClient.Send(message);

Send an email with a file and each time the file name is with the current time

I have a task for executions to send a file that has been downloaded from the database
and I will have to send the file by e-mail with the name of the time the file was sent.
what would be the order of execution of this task in integrated services
with the use of file system task and send email task in ssis.
No doubt, you should create your with file system task at first and save it where you wish. after that with using "Script Task" in control flow, you can send it easily. You just add these sources in "using namespaces" block:
"using System.Net.Mail;"
The Code you need to add in public main method should be like this:
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
string sSubject = [Your Subject Name String];
string sBody = [Your Body String];
string sEmailServer = [The Address of your Email Server];
string sEmailPort = [The Email Port];
string sEmailUser = [Your Email address];
string sEmailPassword = [Your Email Password];
string sEmailSendTo = [The Receiver Email];
string sEmailSendCC = [The CC Sender Email];
string sEmailSendFrom = [The Sender Email];
string sEmailSendFromName = [The Sender Name];
message.Priority = MailPriority.High;(Normal, Low, High).
MailAddress fromAddress = new MailAddress(sEmailSendFrom, sEmailSendFromName);
message.From = fromAddress;
string[] sEmailTo = Regex.Split(sEmailSendTo, ";");
string[] sEmailCC = Regex.Split(sEmailSendCC, ";");
int sEmailServerSMTP = int.Parse(sEmailPort);
smtpClient.Host = sEmailServer;
smtpClient.Port = sEmailServerSMTP;
smtpClient.EnableSsl = false;
System.Net.NetworkCredential myCredentials =
new System.Net.NetworkCredential(sEmailUser, sEmailPassword);
smtpClient.Credentials = myCredentials;
if (sEmailTo != null)
{
for (int i = 0; i < sEmailTo.Length; ++i)
{
if (sEmailTo[i] != null && sEmailTo[i] != "")
{
message.To.Add(sEmailTo[i]);
}
}
}
if (sEmailCC != null)
{
for (int i = 0; i < sEmailCC.Length; ++i)
{
if (sEmailCC[i] != null && sEmailCC[i] != "")
{
message.To.Add(sEmailCC[i]);
}
}
}
Attachment myAttachment = new Attachment([The path the file you saved]);
message.Attachments.Add(myAttachment);
message.Subject = sSubject;
message.IsBodyHtml = false;
message.Body = sBody;
smtpClient.Send(message);

SMTP is not sending emails in gmail business account

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;
}

How can I add an attachment to a MailMessage?

I've got this code that sends a simple email using SmtpClient, MailMessage, and MailAddress objects:
private void EmailMessage(string msg)
{
string TO_EMAIL = "cshannon#proactusa.com";
var windowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent();
string userName = windowsIdentity.Name;
string subject = string.Format("Log msg from Report Runner app sent {0}; user was {1}", DateTime.Now.ToLongDateString(), userName);
string body = msg;
var SmtpServer = new SmtpClient(ReportRunnerConstsAndUtils.EMAIL_SERVER);
var SendMe = new MailMessage();
SendMe.To.Add(TO_EMAIL);
SendMe.Subject = subject;
SendMe.From = new MailAddress(ReportRunnerConstsAndUtils.FROM_EMAIL);
SendMe.Body = body;
try
{
SmtpServer.UseDefaultCredentials = true;
SmtpServer.Send(SendMe);
}
}
I also need, though, to attach a file to an email. I was doing it using Outlook like so:
Application app = new Application();
MailItem mailItem = app.CreateItem(OlItemType.olMailItem);
. . .
FileInfo[] rptsToEmail = GetLastReportsGenerated();
foreach (var file in rptsToEmail)
{
String fullFilename = String.Format("{0}\\{1}", uniqueFolder, file.Name);
if (!file.Name.Contains(PROCESSED_FILE_APPENDAGE))
{
mailItem.Attachments.Add(fullFilename);
}
}
mailItem.Importance = OlImportance.olImportanceNormal;
mailItem.Display(false);
...but I need to move away from using Outlook for this. Here the MailItem is a Microsoft.Office.Interop.Outlook.MailItem
How can I add attachments in the simple MailMessage I need to use now?
Setting the Importance is not too important, I don't think, but the Display is something I will need to set for the MailMessage, too.
Easy:
if (!file.Name.Contains(PROCESSED_FILE_APPENDAGE))
{
var attachment = new Attachment(fullFilename);
mailMsg.Attachments.Add(attachment);
}
mailMsg.Priority = MailPriority.Normal;

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");
}