Hi I know how to send the mail from smpt through below mailcode.But not getting any clue on how to send mail from user's outlook.so that user can find his mails in his sent items folder
Please help me..
Below is web config codes for sending the mail
<mailSettings>
<smtp>
<network host="11.111.111.1" port="25" defaultCredentials="true"/>
</smtp>
</mailSettings>
and this is my send mail method:
public static void SendMessage(string sbj, string bd, string bccadd, string attachFile1,string buyeremail)
{
MailMessage message = new MailMessage();
SmtpClient client = new SmtpClient();
message.From = new MailAddress(buyeremail);
message.To.Add(new MailAddress(bccadd));
message.Subject = sbj.Trim();
message.Body = bd.Trim();
SmtpClient mysmptclient = new SmtpClient();
mysmptclient.DeliveryMethod = SmtpDeliveryMethod.Network;
message.Attachments.Add(new Attachment(attachFile1));
try
{
message.Attachments.Add(new Attachment(attachFile5));
}
catch
{
}
mysmptclient.Send(message);
}
I just modified my code as below :
try
{
Outlook.Application oApp = new Outlook.Application();
Outlook.NameSpace oNamespace = new Outlook.NameSpace("MAPI");
Outlook.MailItem oMailItem = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
oMailItem.HTMLBody = bd.Trim();
oMailItem.Subject = sbj.Trim();
Outlook.Recipients oRecips = (Outlook.Recipients)oMailItem.Recipients;
Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(bccadd);
oRecip.Resolve();
oMailItem.Send();
oRecip = null;
oRecips = null;
oMailItem = null;
oApp = null;
}
catch (Exception ex)
{
Response.Write("<script>alert('" + ex.Message + "');</script>");
//string script = "<script>alert('" + ex.Message + "');</script>";
}
Bu now it shows error:
Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).
Please help me
You can use the Microsoft Exchange Web Services (EWS) Managed API to create and send email messages.
http://msdn.microsoft.com/en-us/library/office/dd633628(v=exchg.80).aspx
Code shown on MSDN:
// Create an email message and identify the Exchange service.
EmailMessage message = new EmailMessage(service);
// Add properties to the email message.
message.Subject = "Interesting";
message.Body = "The merger is finalized.";
message.ToRecipients.Add("user1#contoso.com");
// Send the email message and save a copy.
message.SendAndSaveCopy();
I believe you'll need to use the outlook API's to perform that, since the MailObject and SMTP will send the mail internally using the mentioned params, something like this should help http://www.codeproject.com/Tips/165548/C-Code-snippet-to-send-an-Email-with-attachment-fr
A possible duplicate : Can only send email via Outlook if Outlook is open
Related
I am trying to send mail through Gmail. I am sending mail successfully when I am testing on localhost, but this does not work when I upload it to a web host. I am seeing this type of error:
Request for the permission of type System.Net.Mail.SmtpPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 failed.
Whenever I am using port 25 get this type of error below:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
Below is my code of send email.
MailMessage mail = new MailMessage("host#gmail.com","User#gamil.com");
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.Subject = "Any String"
mail.Body = mailbody;
mail.IsBodyHtml = true;
SmtpServer.Port = 587;
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new System.Net.NetworkCredential("xyz#gmail.com","123");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
Is there any solution? Please suggest to me!
Edit: OP Added extra information crucial to answering this question, but I'm keeping the old answer around as it might still help someone
New Answer:
This StackOverflow question already answered this question
OldAnswer:
As this StackOverflow answer already answered, you changed the Port on the SMTP Server to 587 instead of its default (25) and this requires elevated permissions causing this error change this:
SmtpServer.Port = 587;
to this:
SmtpServer.Port = 25;
and it should work
Note: When using SSL the port needs to be 443
Answer : Your code add SmtpDeliveryFormat.SevenBit
Example:
using (SmtpClient smtp = new SmtpClient())
{
NetworkCredential credential = new NetworkCredential
{
UserName = WebConfigurationManager.AppSettings["UserName"],
Password = WebConfigurationManager.AppSettings["Password"],
};
smtp.Credentials = credential;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.DeliveryFormat = SmtpDeliveryFormat.SevenBit;
smtp.Host = WebConfigurationManager.AppSettings["Host"];
smtp.Port = WebConfigurationManager.AppSettings["Port"].ToNcInt();
smtp.EnableSsl = Convert.ToBoolean(WebConfigurationManager.AppSettings["EnableSsl"]);
smtp.Send(mail);
}
Try This
using System;
using System.Net;
using System.Net.Mail;
namespace AmazonSESSample
{
class Program
{
static void Main(string[] args)
{
// Replace sender#example.com with your "From" address.
// This address must be verified with Amazon SES.
String FROM = "a#a.com";
String FROMNAME = "ABC";
// Replace recipient#example.com with a "To" address. If your account
// is still in the sandbox, this address must be verified.
String TO = "a#a.com";
// Replace smtp_username with your Amazon SES SMTP user name.
String SMTP_USERNAME = "a#a.com";
// Replace smtp_password with your Amazon SES SMTP user name.
String SMTP_PASSWORD = "ASJKAJSN";
// (Optional) the name of a configuration set to use for this message.
// If you comment out this line, you also need to remove or comment out
// the "X-SES-CONFIGURATION-SET" header below.
String CONFIGSET = "ConfigSet";
// If you're using Amazon SES in a region other than US West (Oregon),
// replace email-smtp.us-west-2.amazonaws.com with the Amazon SES SMTP
// endpoint in the appropriate AWS Region.
String HOST = "smtp-relay.sendinblue.com";
// The port you will connect to on the Amazon SES SMTP endpoint. We
// are choosing port 587 because we will use STARTTLS to encrypt
// the connection.
int PORT = 587;
// The subject line of the email
String SUBJECT =
"Amazon SES test (SMTP interface accessed using C#)";
// The body of the email
String BODY =
"<h1>Amazon SES Test</h1>" +
"<p>This email was sent through the " +
"<a href='https://aws.amazon.com/ses'>Amazon SES</a> SMTP interface " +
"using the .NET System.Net.Mail library.</p>";
// Create and build a new MailMessage object
MailMessage message = new MailMessage();
message.IsBodyHtml = true;
message.From = new MailAddress(FROM, FROMNAME);
message.To.Add(new MailAddress(TO));
message.Subject = SUBJECT;
message.Body = BODY;
// Comment or delete the next line if you are not using a configuration set
message.Headers.Add("X-SES-CONFIGURATION-SET", CONFIGSET);
using (var client = new System.Net.Mail.SmtpClient(HOST, PORT))
{
// Pass SMTP credentials
client.Credentials =
new NetworkCredential(SMTP_USERNAME, SMTP_PASSWORD);
// Enable SSL encryption
client.EnableSsl = true;
// Try to send the message. Show status in console.
try
{
Console.WriteLine("Attempting to send email...");
client.Send(message);
Console.WriteLine("Email sent!");
}
catch (Exception ex)
{
Console.WriteLine("The email was not sent.");
Console.WriteLine("Error message: " + ex.Message);
}
}
}
}
}
Is there any way to configure forward-to email address like replyto in System.Net.Mail.MailMessage?
If not then is there any way I can achieve that?
No, there is not.
The reason is that there is no such field defined in an email. You can see the defined fields here.
So this is not a matter of C# API, there is no way to do what you want to do, not in C# and not in other languages/frameworks.
You can do this only by doing it via Outlook (using interop)
var newItem = mailItem.Forward();
newItem.Recipients.Add("test#test.be");
newItem.Send();
the .Forward() is described here
There's no 'forwardto' in MailMessage (that's up to the user receiving the mail).
But you can send to multiple receipients at once, using either the CC or the BCC properties.
Here's an axample using CC:
public static void CreateCopyMessage(string server)
{
MailAddress from = new MailAddress("ben#contoso.com", "Ben Miller");
MailAddress to = new MailAddress("jane#contoso.com", "Jane Clayton");
MailMessage message = new MailMessage(from, to);
// message.Subject = "Using the SmtpClient class.";
message.Subject = "Using the SmtpClient class.";
message.Body = #"Using this feature, you can send an email message from an application very easily.";
// Add a carbon copy recipient.
MailAddress copy = new MailAddress("Notification_List#contoso.com");
message.CC.Add(copy);
SmtpClient client = new SmtpClient(server);
// Include credentials if the server requires them.
client.Credentials = CredentialCache.DefaultNetworkCredentials;
Console.WriteLine("Sending an email message to {0} by using the SMTP host {1}.",
to.Address, client.Host);
try {
client.Send(message);
}
catch (Exception ex) {
Console.WriteLine("Exception caught in CreateCopyMessage(): {0}",
ex.ToString() );
}
}
Now you can send to more than one, which Works like auto forwarding.
This is my first experience with sending an email using c#. Everything I have so far I got from reading and watching videos. I currently have the code written to send an email. It creates it and displays the email with all of the information correct and ready to send. The email opens up and then I click send and it works. The problem is that I want the email to send on its own, without me having to click send.
This is my code:
static void SendEmail()
{
Microsoft.Office.Interop.Outlook.Application app = new
Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem mailItem = app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem) as Outlook.MailItem;
mailItem.Subject = "Status of Code";
mailItem.To = "bt#outlook.com";
mailItem.Body = "Code Ran Successfully";
mailItem.Importance = Outlook.OlImportance.olImportanceHigh;
mailItem.Display(false);
}
I tried adding in
mailItem.Send;
but I kept getting an error. How else would I do this?
Referencing Microsofts documentation (https://msdn.microsoft.com/en-us/library/swas0fwc(v=vs.110).aspx), I'd recommend instead using an SMTP client (as opposed to being outlook specific).
Your code would then look something like:
public static void SendEmail()
{
string to = "bt#outlook.com";
string from = "fromAddress#outlook.com";
MailMessage message = new MailMessage(from, to);
message.Subject = "Using the new SMTP client.";
message.Body = #"Code Ran Successfully";
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = true;
try {
client.Send(message);
}
catch (Exception ex) {
Console.WriteLine("Exception caught in SendEmail(): {0}",
ex.ToString() );
}
}
I caught into big problem since am new to SMTP Email Server.
I have installed smtp server in my web server and configured the required details. My emails are now sending but in spam
I have implemented SendMail code using c# in my webapplication and I have one clarification on that.
string mailFrom = "Newsletter#my-domain.com";
string message = string.Empty;
System.Net.Mail.MailMessage email = new MailMessage(mailFrom, EmailAddress);
email.Subject = "Mail from my-domain.com";
email.Body = message;
email.IsBodyHtml = true;
email.Priority = MailPriority.High;
System.Net.Mail.SmtpClient mailClient = new SmtpClient();
System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("username", "password");
mailClient.Host = "my-mail-server-domain.com";
mailClient.Port = 25;
mailClient.EnableSsl = false;
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = basicAuthenticationInfo;
try
{
mailClient.Send(email);
}
catch (Exception ex)
{
log4net.ILog logger = log4net.LogManager.GetLogger("File");
logger.Error(ex.ToString());
}
What value should I give for mailFrom. Is valid email is required for From address? Does it cause sending mail as spam? I don't have emailid in the name of newsletter#my-domain.com. What shall I do for that?
Please anyone clarify on this.
It is the responsibility of the mail server the validation of the address, all could be good or bad for c#. I think your focus is wrong.
The from should be a valid email address. That is the email from where an email is sent. The network credentials that you specify must match with the from address. Once you provide a valid email address, the emails will not go to spam folder hopefully.
I am using the code as described in this question. However get following error when send an email.
Mailbox unavailable. The server response was: please authenticate to
use this mail server
Any ideas what could be wrong?
UPATE: Here is the code
System.Net.Mail.SmtpClient Client = new System.Net.Mail.SmtpClient();
MailMessage Message = new MailMessage("From", "To", "Subject", "Body");
Client.Send(Message);
With following in App.config.
<system.net>
<mailSettings>
<smtp from="support#MyDomain1.com">
<network host="smtp.MyDomain1.com" port="111" userName="abc" password="helloPassword1" />
</smtp>
</mailSettings>
</system.net>
The code posted there should work. if it doesn't, you might try setting the username and password in code-behind rather than reading them from the web.config.
Code sample from systemnetmail.com:
static void Authenticate()
{
//create the mail message
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("me#mycompany.com");
mail.To.Add("you#yourcompany.com");
//set the content
mail.Subject = "This is an email";
mail.Body = "this is the body content of the email.";
//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1");
//to authenticate we set the username and password properites on the SmtpClient
smtp.Credentials = new NetworkCredential("username", "secret");
smtp.Send(mail);
}
Yes, the smtp server is telling you that in order to relay email for you, you need to authenticate before attempting to send the email. If you have an account with the smptp server, you can set the credentials on the SmtpClient object accordingly. Depending on the authentication mechanism supported by the smtp server, the port, etc, will be different.
Example from MSDN:
public static void CreateTestMessage1(string server, int port)
{
string to = "jane#contoso.com";
string from = "ben#contoso.com";
string subject = "Using the new SMTP client.";
string body = #"Using this new feature, you can send an e-mail message from an application very easily.";
MailMessage message = new MailMessage(from, to, subject, body);
SmtpClient client = new SmtpClient(server, port);
// Credentials are necessary if the server requires the client
// to authenticate before it will send e-mail on the client's behalf.
client.Credentials = CredentialCache.DefaultNetworkCredentials;
try {
client.Send(message);
}
catch (Exception ex) {
Console.WriteLine("Exception caught in CreateTestMessage1(): {0}",
ex.ToString() );
}
}
The bottom line is that your credentials are not being passed to the Smtp server or else you wouldn't be getting that error.