I am trying to send an email as text to end users mobile, the code is running fine but I can't seems to get the SMS.
Here is my code
public Task SendAsync()
{
var fromAddress = new MailAddress("test#gmail.com", "Authenticator");
var toAddress = new MailAddress("mynumber#sms.sancharnet.in", "User");
const string fromPassword = "mypassword";
const string subject = "subject";
string body = Session["PIN"].ToString();
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
IsBodyHtml = false
})
{
smtp.Send(message);
}
return Task.FromResult(0);
}
Is there something I am missing here ? or is this not how we try to do an Email to text ?
Related
I send an email via SMTP google and my code is working fine. What I want is to set this email does not accept replies in the SMTP setting.
Working Code
public ActionResult SendEmail()
{
var fromAddress = new MailAddress("from#gmail.com", "From Name");
var toAddress = new MailAddress("to#gmail.com", "To Name");
const string fromPassword = "password";
const string subject = "Subject";
const string body = "<p>Hello</p>";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
IsBodyHtml = true
})
{
smtp.Send(message);
}
return View();
}
Google Mailbox
I don't want the Reply button highlighted in blue
As far as I am aware, it is not possible through SMTP to disable a client's reply function.
Instead, you could create a no-reply address with a vacation responder to alert individuals that the inbox is not being monitored (possibly including where to go if a user does need to reach out to someone).
for debugging purposes, I have globally handled all exceptions. Whenever an exception occurs, I silently handle it, and want to send myself an e-mail with the error details, so I can address this issue.
I have two emails, email1#gmail.com, and email2#gmail.com...
I have attempted using this code to send myself an e-mail, but it is not working.
string to = "email1#gmail.com";
string from = "email2#gmail.com";
string subject = "an error ocurred";
string body = e.ToString();
MailMessage message = new MailMessage(from, to, subject, body);
SmtpClient client = new SmtpClient("smtp.google.com");
client.Timeout = 100;
client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.Send(message);
I have tried countless other pieces of code but I have no idea how to do it. Does anyone have a solid solution for this? Thanks a bunch.
This has to work. See more info here: Sending email in .NET through Gmail
using System.Net;
using System.Net.Mail;
//...
var fromAddress = new MailAddress("alextodorov01#abv.bg", "From Name");
var toAddress = new MailAddress("kozichka01#abv.bg", "To Name");
const string fromPassword = "fromPassword";
const string subject = "an error ocurred";
const string body = e.ToString();
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
//...
I am trying to email from c# the problem is when iam formatting in my body doesnt seem to be working there any solution where i can use this break line also i need to create table in mail body. Below is my code where the body1 is my body of email content.
var fromAddress = new MailAddress("something#gmail.com", "Name");
const string fromPassword = "pwd123";
const string subject = "System generated test mail ";
string email = bind_email(analyst);
string body1 = "Hi " + analyst.ToString();
body1 = body1 + "<br/>";
body1 = body1 + " This is system generated test mail for " + Session["TaskAssigned"].ToString();
body1 = body1 + " To be competed before" + Session["Enddate"].ToString() + "<br/><br/> ";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage()
{
From = fromAddress,
Subject = subject,
Body = body1,
})
{
message.To.Add(email);
smtp.Send(message);
}
ust put IsBodyHtml =true in MailMessage Settings
mailMessage = new MailMessage()
{
From = new MailAddress(senderAddress),
Subject = subject,
Body = message,
IsBodyHtml = true
};
Use html tags instead of plain text inside message.
Hope this helps.
Your problem is you didn't assign MailMessage.IsBodyHtml to true. You can format your body using a true html.
I used to format my generated email coming from html. First create a test.html:
<html>
<body>
<p>
Hi #analyst#
<br/>
This is a system generated test mail for #task_assigned# to be completed before #enddate#
<br/>
<br/>
</p>
</body>
</html>
Then just replace the needed data.
Here's your code I edited:
var fromAddress = new MailAddress("something#gmail.com", "Name");
const string fromPassword = "pwd123";
const string subject = "System generated test mail ";
string email = bind_email(analyst);
System.IO.StreamReader sr = new System.IO.StreamReader(Server.MapPath("~/App_Data/test.html"));
string body1 = sr.ReadToEnd();
body1 = body1.Replace("#analyst#", analyst.ToString());
body1 = body1.Replace("#task_assigned#", Session["TaskAssigned"].ToString());
body1 = body1.Replace("#enddate#", Session["Enddate"].ToString());
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage()
{
From = fromAddress,
Subject = subject,
Body = body1,
IsBodyHtml = true
})
{
message.To.Add(email);
smtp.Send(message);
}
Hope this helps
I was following this: post
And after a few seconds realized that the body is a constant and I can't pass a string to it. Is there any fast way to change this code a bit and get what I need?
public void PostMessage(string body,string subject)
{
var fromAddress = new MailAddress("from#gmail.com", "From Name");
var toAddress = new MailAddress("to#example.com", "To Name");
const string fromPassword = "fromPassword";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
})
{
smtp.Send(message);
}
}
you can call it like this:
PostMessage("MAH BODY", "SUBJECT");
You remove the const bit...
const string body = "Body";
turns into:
string body = bodyPassedIn; //where bodyPassedIn = is being passed into the method
You don't even need the variable at all:
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = bodyPassedIn // here!
})
{
smtp.Send(message);
}
The code below works when using on local machine, but fails when published on server (HostGator) for multiple emails in to. It also works if I am trying to send email to just one person.
public void SendMailToMultiples(MailAddress fromAddress, string fromPassword, MailAddressCollection toAddress, string subject, string body)
{
MailMessage message = new MailMessage(){
Subject = subject,
IsBodyHtml = true,
Body = body,
From = fromAddress
};
foreach(var email in toAddress)
{
message.To.Add(email.Address);
}
var smtp = new SmtpClient
{
Host = HOST_NAME,
Port = PORT_NUMBER,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
{
smtp.Send(message);
}
}