Hello i have my code to send datagridview to email and it is running very well.
The problem is that it is just sending to my email, and not to other people email ,
my email is the Network Credential . How can i send it to other people?
Pesquisar_Items pesquisar = new Pesquisar_Items();
var client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Credentials = new NetworkCredential("jpbritopoker#gmail.com", "***");
var mail = new MailMessage();
mail.From = new MailAddress("nervir#epnervir.com");
mail.To.Add(textBox1.Text);
mail.IsBodyHtml = true;
mail.Subject = textBox2.Text;
string mailBody = "<table width='100%' style='border:Solid 1px Black;'>"; ;
foreach (DataGridViewRow row in itemDataGridView.Rows)
{
mailBody += "<tr>";
foreach (DataGridViewCell cell in row.Cells)
{
mailBody += "<td>" + cell.Value + "</td>";
}
mailBody += "</tr>";
}
mailBody += "</table>";
//your rest of the original code
mail.Body = mailBody;
client.Send(mail);
MessageBox.Show("O email foi enviado com sucesso");
this.Close();
I don't think Google's smtp server will allow you to change the sender's email address as you are doing. It would be something typical of someone trying to use their server to send spam. If you change your code to appear as if the email is coming from jpbritopoker#gmail.com it may work. Something like this:
mail.From = new MailAddress("jpbritopoker#gmail.com");
You were doing:
mail.From = new MailAddress("nervir#epnervir.com");
did you try something like this
mail.To.Add("foo1#dn.com")
mail.To.Add("foo2#dn.com")
mail.To.Add("foo3#dn.com")
or
mail.CC.Add("foo3#dn.com")
Related
I want to open mail to and inside the body of my email I want to create a table and insert values inside my model. So I execute outlook like this:
var mail = $"mailto:test#test.com?subject=ProjectListTest&body={finalString}";
My question is, how can I create a table and add to body of mailto?
Table headers: Name, Customer
so inside each row I want to use something like:
var finalString = string.Empty;
foreach(var customer in CustomerList)
{
finalString = finalString + customer.Name + customer.CustomerKey
}
Is it possible to achieve this? what is the correct format to create a table in Outlook. Regards
pIf the table would be created using the html mail body format, then you can use the following method to generate it:
public string GenerateMailBodyWithTable(List<Customer> customers)
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append($"<html>{ Environment.NewLine }<body>{ Environment.NewLine }");
if (customers.Count > 0)
{
stringBuilder.Append($"<table><tr><th>Name</th><th>Key</th></tr>{ Environment.NewLine }");
foreach (Customer customer in customers)
{
stringBuilder.Append($"<tr><th>{ customer._name }</th><th>{ customer._key }</th></tr>{ Environment.NewLine }");
}
stringBuilder.Append($"<table>{ Environment.NewLine }");
}
else
{
stringBuilder.Append($"<p>No customers<p>{ Environment.NewLine }");
}
stringBuilder.Append($"</html>{ Environment.NewLine }</body>");
return stringBuilder.ToString();
}
After generating the html body you can perform the following action to fill the mailbody:
Outlook.Application outlookApp = new Outlook.Application();
Outlook.MailItem mailMessage = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
mailMessage.HTMLBody = GenerateMailBodyWithTable(customers);
mailMessage.Display(true);
Don't forget to place this using statement:
using Outlook = Microsoft.Office.Interop.Outlook;
First at all, you have to create your custom HTML:
string finalString = "<table><tr><td><b>Name</b></td><td><b>Customer</b></td></tr>";
foreach(var customer in CustomerList)
{
finalString += "<tr><td>" + customer.Name + "</td><td>" + customer.CustomerKey + "</td></tr>";
}
finalString += "</table>";
If you are using WinForms and you want to send an email with this body, you can use MailMessage Class from System.Net.Mail and sending it with SmtpClient. This way:
MailMessage mail = new MailMessage("from", "mailto", "Subject", finalString);
mail.IsBodyHtml = true; //Important
SmtpClient smtp = new SmtpClient("serverSMTP");
smtp.EnableSsl = USE_SSL;
smtp.Port = YOUR_PORT;
smtp.Credentials = new System.Net.NetworkCredential("email", "password");
smtp.Send(correo);
If you want to simulate a "mailto" action, you can use:
string command = $"mailto:test#test.com?subject=ProjectListTest&body={finalString}";
Process.Start(command);
Regards
I'm trying to send a mail containing html with inline images as well as a calender invitation (.ics). I have tested that both elements at least work seperately.
If I use Gmail.com to view the mail it displays all elements correctly. Showing information about the event at the top of the mail and showing the text with images. The issue is that depending on the order that I create the alternateViews, it either works with Outlook or Iphone, it always works with gmail.com/gmailclient but not all three at the same time.
SmtpClient sc = new System.Net.Mail.SmtpClient(IP);
MailMessage msg = new MailMessage();
msg.From = new System.Net.Mail.MailAddress("demomail#hotmail.com", "bob");
string mailTo = "client#gmail.com";
msg.To.Add(new MailAddress(mailTo, "Client Name"));
msg.Subject = "Send Calendar Appointment Email";
string mailBody = $"<br><div class=\"dagsordenMødeDato\"> Her er dagsordenen for mødet 10-10-2018 14:45:00 </div><br> <br><ul class=\"dagsordenListe\"><li>Leasing til dig</li><li>Pension</li><li>Garantkunde</li><li>Dine fordele</li></ul><br> <div class=\"bekrivelseAfVedhæftning\">Jeg har vedhæftet et dokument.</div>";
string icsContent = System.IO.File.ReadAllText(#"H:\hjemmelavetICS.ics");
If the alternateView for the calender is placed before the alternateView for Html in the code then it works on 'Mail' for iphone
ContentType contype = new ContentType("text/calendar");
contype.Parameters.Add("method", "REQUEST");
contype.Parameters.Add("name", "Invitation.ics");
contype.CharSet = "utf-8";
AlternateView avCal =
AlternateView.CreateAlternateViewFromString(icsContent, contype);
msg.AlternateViews.Add(avCal);
LinkedResource inlineRådgiver = new LinkedResource(
#"K:\MEDARBEJDERBILLEDER\Signatur Notes\" + "fn" + ".jpg",
MediaTypeNames.Image.Jpeg);
inlineRådgiver.ContentId = "raadgiverID";
inlineRådgiver.ContentType.Name = "Raadgiver.jpg";
LinkedResource inlineKampagne = new LinkedResource(
#"K:\MEDARBEJDERBILLEDER\Signatur Notes\" + "Kampagne" + ".jpg",
MediaTypeNames.Image.Jpeg);
inlineKampagne.ContentId = "kampagneID";
inlineKampagne.ContentType.Name = "Kampagne.jpg";
var htmlContentType =
new System.Net.Mime.ContentType("text/html");
htmlContentType.CharSet = "utf-8";
string embededHtml =
"<img src=cid:raadgiverID alt=\"medarbejder\"></p><img style=\"border: 0;\" src=cid:kampagneID alt=\"kampagne\">";
string fullHtml = mailBody + embededHtml;
var avHtmlBody = AlternateView.CreateAlternateViewFromString(fullHtml, htmlContentType);
avHtmlBody.LinkedResources.Add(inlineRådgiver);
avHtmlBody.LinkedResources.Add(inlineKampagne);
msg.AlternateViews.Add(avHtmlBody);
sc.Send(msg);
However if I add the alternateview for html before the calender then it works for Outlook.
I have been scratching my head over this for a week now, any help would be appreciated.
I have anasp.net web form which on click of a button sends i want an email to be sent to my Microsoft outlook email account. I have done this on other
websites but that was using hotmail but all external web email providers are blocked by the companies firewall (as i tried setting up a gmail account) so
i need to use Outlook but i have not idea on how to implent this and solutions i have seen on Google dont seem to work. I dont know if it'll make a
difference or not but i have been informed that the users password epxires every 30days so i suspect i'll need to use windows authenication or something
but not sure.
I'm not sure on how Outlook send the email as i know from past experience from using hotmail that the email is just sent on click of the button but i'm
not sure if outlook would open the email window for the user to click the send button. If it does, i need the information captured on the webform to be
containd in the email and the content of the email body not to be altered (if this can be done, again not ot sure if it can but not a problem if it
can't).
Below is the code i used for when i tried gmail but as i said i was told it wouldnt be allowed.
using System.Configuration;
using System.Net.Mail;
using System.Net;
using System.IO;
protected void BtnSuggestPlace_Click(object sender, EventArgs e)
{
#region Email
try
{
//Creates the email object to be sent
MailMessage msg = new MailMessage();
//Adds your email address to the recipients
msg.To.Add("MyEmailAddress#Test.co.uk");
//Configures the address you are sending the email from
MailAddress address = new MailAddress("EmailAddress#Test.com");
msg.From = address;
//Allows HTML to be used when setting up the email body
msg.IsBodyHtml = true;
//Email subjects title
msg.Subject = "Place Suggestion";
msg.Body = "<b>" + lblPlace.Text + "</b>" + " " + fldPlace.Text
+ Environment.NewLine.ToString() +
"<b>" + lblLocation.Text + "</b>" + " " + fldLocation.Text
+ Environment.NewLine.ToString() +
"<b>" + lblName.Text + "</b>" + " " + fldName.Text;
//Configures the SmtpClient to send the mail
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.EnableSsl = true; //only enable this if the provider requires it
//Setup credentials to login to the sender email address ("UserName", "Password")
NetworkCredential credentials = new NetworkCredential("MyEmailAddress#Test.co.uk", "MyPassword");
client.Credentials = credentials;
//Send the email
client.Send(msg);
}
catch
{
//Lets the user know if the email has failed
lblNotSent.Text = "<div class=\"row\">" + "<div class=\"col-sm-12\">" + "There was a problem sending your suggestion. Please try again."
+ "</div>" + "</div>" + "<div class=\"form-group\">" + "<div class=\"col-sm-12\">" + "If the error persists, please contact Antony." + "</div>" +
"</div>";
}
#endregion
}
edit 2: Now we have established your going through exchance this is how my code has always worked
SmtpClient sptmClient = new SmtpClient("exchange server name")
MailMessage m = new MailMessage();
m.To.Add(new MailAddress("Address"));
m.From = new MailAddress("");
m.Subject = "";
m.Body = "";
m.IsBodyHtml = true;
sptmClient.Send(m);
but there is another answer on here that uses outlook interoperlation that might work better for you
With Exchange it must work.
test this :
using Outlook = Microsoft.Office.Interop.Outlook;
private void SendWithExchange()
{
Outlook.Application oApp = new Outlook.Application();
Outlook.MailItem mail = oApp.CreateItem(
Outlook.OlItemType.olMailItem) as Outlook.MailItem;
mail.Subject = "Exemple à tester";
Outlook.AddressEntry currentUser =
oApp.Session.CurrentUser.AddressEntry;
if (currentUser.Type == "EX")
{
Outlook.ExchangeUser manager =
currentUser.GetExchangeUser();
mail.Recipients.Add(manager.PrimarySmtpAddress);
mail.Recipients.ResolveAll();
//mail.Attachments.Add(#"c:\sales reports\fy06q4.xlsx",
// Outlook.OlAttachmentType.olByValue, Type.Missing,
// Type.Missing);
mail.Send();
}
}
in 3.5 .Net Framework i have a problem to sending an email on server.
Code-
MailMessage message = new MailMessage();
message.From=new MailAddress("CMC Enquiry" + "<my1#domain.in>");
message.To.Add(new MailAddress("vishalpatel8086#gmail.com"));
message.CC.Add(new MailAddress("varun_aone#yahoo.com"));
message.Subject = "Enquiry from CMC site";
string p = "<b>Name: </b>" + TextBox1.Text;
p += "<br><b>Mobile:</b> " + TextBox3.Text;
p += "<br><b>Mail ID:</b> " + TextBox2.Text;
p += "<br><b>Address:</b> " + TextBox4.Text;
p += "<br><b>City:</b> " + TextBox5.Text;
p += "<br><b>Location:</b>" + locationlst.SelectedItem.Text;
p += "<br><b>College:</b> " + TextBox11.Text;
p += "<br><b>Course:</b> " + DropDownList3.SelectedItem.Text;
p += "<br><b>Query:</b> " + TextBox12.Text;
message.Body = p;
message.IsBodyHtml = true;
SmtpClient SMTPServer = new SmtpClient("localhost");
// try
// {
SMTPServer.Send(message);
//result = "Your Enquiry has been Submitted !!";
Label5.Text = "Your Enquiry has been Submitted !!";
//Response.Write("<script language=JavaScript> alert('Your Enquiry has been Submitted !!'); </script>");
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
TextBox5.Text = "";
TextBox11.Text = "";
TextBox12.Text = "";
DropDownList3.SelectedIndex = 0;
// }
// catch
// {
// Label5.Text = "Low Server Problem !!Your Enquiry Not Submitted";
//Response.Write("<script language=JavaScript> alert('Server Problem !!Your Enquiry Not Submitted'); </script>");
// }
}
else
{
Label5.Text = "Server Problem !!Your Enquiry Not Submitted";
}
}
This code is working my other web hosting server with my different website but is not working with new web site . A error is being occured like -
Bad sequence of commands. The server response was: This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server.
Error Is -
Line 83: SMTPServer.Send(message);
Source File: c:\inetpub\vhosts\cmcent.in\httpdocs\enquiry.aspx.cs
Line: 83
Your SmtpClient code needs to be revised as follow:
//Set your smtp client settings
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.yourdomain.com"; //set with your smtp server
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential("fromEmail#yourdomain.com", "fromEmailPassword");
smtp.Timeout = 20000;
}
// Now send your email with this smtp
smtp.Send(message);
Optionally you can also try to enable/disable ssl & changing the port.
//Create the session.
Outlook.Application application = new Outlook.Application();
//Create the session.
Outlook.MailItem mail = application.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
//create the receipents object
Outlook.Recipients objOutlookRecip = mail.Recipients;
while (Recipients.Read())
{
mail.Subject = Confirmations.GetString(2);
mail.Body = Confirmations.GetString(4) + ("\r\n");
mail.Body = mail.Body + ("\r\n") + Confirmations.GetString(5) + ("\r\n");
mail.Body = mail.Body + ("\r\n") + Confirmations.GetString(6);
mail.SentOnBehalfOfName = "user#email.com";
mail.Recipients.Add(Recipients.GetString(8));
}
Does anyone know how i might be able to insert the email addresses into bcc instead of the current process in the to field
Outlook.Recipient recipBcc =
mail.Recipients.Add(Recipients.GetString(8));
recipBcc.Type = (int)Outlook.OlMailRecipientType.olBCC;
check OlMailRecipientType Enumeration