I have created a WebApplication which is used by my Team Members. They are accessing WebApp with a tunnel link on their PC. There is a function where Team Mates send email by clicking on button. This function is working on fine for me, but when they try using this function Outlook triggers email from my Outlook which is on my PC instead of their individual Outlook account.
Here is the code to trigger email
using (Db db = new Db())
{
Ticket newDTO = db.Tickets.Find(id);
Application app = new Application();
MailItem mailItem = app.CreateItem(OlItemType.olMailItem);
mailItem.Subject = "Feedback: Ticket Escalated " + newDTO.CaseId + " For Customer " + newDTO.EscalatedOn;
mailItem.To = newDTO.Email;
mailItem.CC = "lead#mydomain.com";
mailItem.HTMLBody = "Hello " + newDTO.Number + "<b>Feedback:</b>" + "<br /><br />" + newDTO.HowCanWeDeEscalate;
mailItem.Importance = OlImportance.olImportanceHigh;
mailItem.Display(false);
mailItem.Send();
}
How can I make this code work? So the email is sent by their individual outlook email and not mine.
Try something like this (You need to configure the SMTP according to your server):
using System;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
namespace SO.DtProblem
{
class Program
{
static async Task Main(string[] args)
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("fromemail#mydomain.com");
mail.To.Add("toemail#mydomain.com");
mail.CC.Add("ccemail#mydomain.com");
mail.Subject = "test some subject";
mail.BodyEncoding = Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Body = "some body text here";
//SMTP Conbfiguration
string clientServer = "mail.mydomain.netORwhatever";
string smtpUserName = "fromemail#mydomain.com";
string smtpPassword = "somepassword";
SmtpClient client = new SmtpClient(clientServer);
client.Port = 25; // 587;
client.UseDefaultCredentials = true; //false;
client.Credentials = new System.Net.NetworkCredential(smtpUserName, smtpPassword);
client.EnableSsl = true; //false;
try
{
await client.SendMailAsync(mail);
}
catch (Exception ex)
{
//Handle it
}
finally
{
mail.Dispose();
client.Dispose();
}
}
}
}
Related
I am trying to send email using C# but I am getting below error.
Mailbox was unavailable. The server response was: Relay access denied. Please authenticate.
I am not sure why I am getting this error. Here, I am using smtp2go third party to send this email.
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("mail.smtp2go.com");
mail.From = new MailAddress("myemail#wraptite.com");
mail.To.Add("test1#gmail.com");
mail.Subject = "Test Email";
mail.Body = "Report";
//Attachment attachment = new Attachment(filename);
//mail.Attachments.Add(attachment);
SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential("me", "password");
//SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
I've tried your code and it work fine.
But in my case, to use the smtp server, from(email address of sender) must use the same domain to authenticate.(but gmail is available to this Send emails from a different address or alias)
So, If your SMTP server connect to smtp2go.com, try as below.
SmtpClient SmtpServer = new SmtpClient("mail.smtp2go.com");
mail.From = new MailAddress("myemail#smtp2go.com");
Or if you need to use service of smtp2go, it would be better use rest API.
Here is updated by your comment when using gmail.
Gmail required secure app access. that's a reason why code is not work.
So, there is two options for this.
1. Update your gmail account security
(origin idea from here : C# - 이메일 발송방법)
Go to here and turn on "Less secure app access". after doing this your code will work.(it works)
2. Using "Google API Client Library for .NET."
I think this is not so easy, check this out, I found an answer related with this here
#region SendMail
//Mail Setting
string EmailSubject = "EmailSubject";
string EmailBody = "EmailBody";
try
{
string FromAddress = "abc#gmail.com";
string EmailList = "abc1#gmail.com";
string EmailServer = "ipofemailserver";
using (var theMessage = new MailMessage(FromAddress, EmailList))
{
// Construct the alternate body as HTML.
string body = "EmailBody";
ContentType mimeType = new System.Net.Mime.ContentType("text/html");
// Add the alternate body to the message.
AlternateView alternate = AlternateView.CreateAlternateViewFromString(body, mimeType);
theMessage.AlternateViews.Add(alternate);
theMessage.Subject = EmailSubject;
theMessage.IsBodyHtml = true;
SmtpClient theSmtpServer = new SmtpClient();
theSmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
theSmtpServer.Host = EmailServer;
theSmtpServer.Send(theMessage);
}
}
catch (Exception ex)
{
string AppPath = AppDomain.CurrentDomain.BaseDirectory;
string ErrorPath = AppDomain.CurrentDomain.BaseDirectory + "File\\Error\\";
string OutFileTime = DateTime.Now.ToString("yyyyMMdd");
using (StreamWriter sw = new StreamWriter(ErrorPath + OutFileTime + ".txt", true, System.Text.Encoding.UTF8))
{
sw.WriteLine(DateTime.Now.ToString() + ":");
sw.WriteLine(ex.ToString());
sw.Close();
}
}
#endregion
i developed a simple website that stores in the user name and sends that to my emailid and then downloads a file.File is getting downloaded but not mailing me the username.
try
{
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("mygmailid");
mailMessage.From =new MailAddress("mydomainbasedemailid");
mailMessage.Subject = "ASP.NET e-mail test";
mailMessage.Body = "Hello world,\n\nThis is an ASP.NET test e-mail!";
SmtpClient smtpClient=new SmtpClient("mail.mydomain.com",587);
smtpClient.Send(mailMessage);
Response.Write("E-mail sent!");
}
catch (Exception ex)
{
Response.Write("Could not send the e-mail - error: " + ex.Message);
}
Try to set the Smtp.Credentials like this:
string HOSTLOGIN = "YourHostLogin";
string HOSTPW = "YourTopSecretPasswort";
var credentials =
new System.Net.NetworkCredential() { UserName = HOSTLOGIN, Password = HOSTPW };
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = credentials;
client.EnableSsl = true;
I am using gmail to send email from C# program, my question is that email found under sent items if logged in into gmail.com via browser?
if I am sent an email from c# program and that email fails(bounce back) then Is failover notification found if I am logged in into gmail.com via browser?
if yes , then is there any additional settings to receive that? I want to show that in gmail.com via browser when I am login.
I don't really understand what you are asking but if you just need the code to send emails through gmail it is pretty simple
void SendMail_Click(object sender, EventArgs e)
{
var email = new Dto.IEmail();
if (EmailBody.Text == string.Empty && EmailSubject.Text == string.Empty)
{
XtraMessageBox.Show("please fill email body and subject");
return;
}
email.Body = EmailBody.Text;
email.Subject = EmailSubject.Text;
email.EmailAddress = "email#gmail.com";
email.ToAddress = "email#gmail.com";
try
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress(email.EmailAddress);
message.To.Add(new MailAddress(email.ToAddress));
message.Subject = email.Subject;
message.Body = email.Body + "\n From user " + GlobalClass.UserLogin.USERNAME + "\n with body: " + _reportedfile;
smtp.Port = 587;
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(email.EmailAddress, "gmailpassword");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
}
catch (Exception ex)
{
XtraMessageBox.Show("err: " + ex.Message);
}
}
I know there is a ton of questions and Answers about this and I did read allot but it seems all outdated.
So I have a mobile app that registers a use to a cloud service and then sends a welcome email to the user's email address.
The service part is done in C# WCF Witch also sends the mail
Below is the prototype function for testing for the mail:
static void SendMail()
{
var fromAddress = new MailAddress("gmail account", "App name");
var toAddress = new MailAddress("User email", "User account");
const string fromPassword = "gmail password";
const string subject = "test";
const string body = "Hey now!!";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
Timeout = 20000
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
Console.WriteLine("Sent");
Console.ReadLine();
}
My code used all the things suggested by others. But I still get the error message
An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll
Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Threading;
using System.ComponentModel;
using System.IO;
using System.Net.Mime;
namespace Invoice.WCFService
{
public class EmailSenser
{
public static bool SendEmail(string toMail, Stream stream, string mailBody)
{
bool sent = false;
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("email#gmail.com");
mail.To.Add(toMail);
mail.Subject = "Invoice";
//mail.Body = "Please, see attached file";
mail.Body = mailBody;
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.BodyEncoding = System.Text.Encoding.UTF8;
ContentType ct = new ContentType(MediaTypeNames.Application.Pdf);
Attachment attachment = new Attachment(stream, ct);
ContentDisposition disposition = attachment.ContentDisposition;
disposition.FileName = DateTime.Now.ToString("dd-MM-yyyy") + ".pdf";
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("email#gmail.com", "password");
SmtpServer.EnableSsl = true;
try
{
SmtpServer.Send(mail);
sent = true;
}
catch (Exception sendEx)
{
System.Console.Write("Error: " + sendEx.Message.ToString());
sent = false;
}
finally
{
//DBContext
}
return sent;
}
}
}
It's my fully working code
Use this code...
static void SendMail()
{
var fromAddress = new MailAddress("fromMail", "App name");
var toAddress = new MailAddress("tomail","app");
const string fromPassword = "passwrd";
const string subject = "test";
const string body = "Hey now!!";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
Timeout = 20000,
};
using (var message = new MailMessage(fromAddress, toAddress))
{
message.Subject = subject;
message.Body = body;
smtp.Send(message);
}
Console.WriteLine("Sent");
Console.ReadLine();
}
Hi I need to send an email using template in c# windows application . I had created a template but i am unable to pass the parameters through the html template. Here is the template which i am using.
For this HTML Template i am calling this in my windows application and sending through gmail smtp. I am able to send the mail but unable to pass the parameters into it. Please Help me out.Here is the code where i am calling in my windows application
try
{
using (StreamReader reader = File.OpenText("H:\\Visitor Management_Project\\Visitor Management_Project\\Visitor Management_Project\\EmailTemplate.htm"))
{
SmtpClient SmtpServer = new SmtpClient("173.194.67.108", 587);
SmtpServer.UseDefaultCredentials = false;
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpServer.Credentials = new System.Net.NetworkCredential("ambarishkesavarapu#gmail.com", "*******");
//SmtpServer.Port = 587;
SmtpServer.Host = "smtp.gmail.com";
SmtpServer.EnableSsl = true;
message = new MailMessage();
message.Subject = "Visitor Arrived";
//message.SubjectEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
message.Body = "EmailTemplate.htm";
//message.BodyEncoding = System.Text.Encoding.UTF8;
message.From = new MailAddress("ambarishkesavarapu#gmail.com");
message.To.Add(lblCPEmail.Text);
message.Priority = MailPriority.High;
message.Body = reader.ReadToEnd();
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
SmtpServer.Send(message);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
How to add Parameters into HTML Template where i am getting all the parameters of that in the same page in Textboxes. Please help me out
I think you already discovered the answer yourself, but I will keep posting the answer instead.
In case you are using Windows Forms and not Windows Presentation Forms (The different only the design part and many new features that does not have on Windows Forms), what I have done is like this (to send email):
public void SendEmail(string _from, string _fromDisplayName, string _to, string _toDisplayName, string _subject, string _body, string _password)
{
try
{
SmtpClient _smtp = new SmtpClient();
MailMessage _message = new MailMessage();
_message.From = new MailAddress(_from, _fromDisplayName); // Your email address and your full name
_message.To.Add(new MailAddress(_to, _toDisplayName)); // The recipient email address and the recipient full name // Cannot be edited
_message.Subject = _subject; // The subject of the email
_message.Body = _body; // The body of the email
_smtp.Port = 587; // Google mail port
_smtp.Host = "smtp.gmail.com"; // Google mail address
_smtp.EnableSsl = true;
_smtp.UseDefaultCredentials = false;
_smtp.Credentials = new NetworkCredential(_from, _password); // Login the gmail using your email address and password
_smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
_smtp.Send(_message);
ShowMessageBox("Your message has been successfully sent.", "Success", 2);
}
catch (Exception ex)
{
ShowMessageBox("Message : " + ex.Message + "\n\nEither your e-mail or password incorrect. (Are you using Gmail account?)", "Error", 1);
}
}
And I am using it like this:
SendEmail(textBox2.Text, textBox5.Text, textBox3.Text, "YOUR_FULL_NAME", textBox4.Text, textBox6.Text, "YOUR_EMAIL_PASSWORD");
Here is the image:
May this answer would help you.
Cheers!