Email alert when page is open - asp.net

Is there any way in asp.net that when my page is open on web then an email alert with username should be generate so that i have information that which one is accessing my page
I changed the code like bellow
Public Sub SendMail(ByVal id As String)
Dim message = New MailMessage()
message.IsBodyHtml = True
message.From = New MailAddress("adeel.aslam0#gmail.com")
message.[To].Add(New MailAddress("malik.adeel#shakarganj.com"))
message.Subject = "user access page"
message.Body = "Your Message"
' add id here
Dim client = New SmtpClient("smtp.gmail.com")
client.Send(message)
End Sub
and in web.config like this
<system.net>
<mailSettings>
<smtp from="adeel.aslam0#gmail.com">
<network host="smtp.gmail.com" defaultCredentials="false"
port="587" userName ="adeel.aslam0#gmail.com" password="pass" />
</smtp>
</mailSettings>
</system.net>

You might do something similar..
Modify your web.config file to send emails
e.g. This use gmail settings
<system.net>
<mailSettings>
<smtp from="yourMailId#gmail.com ">
<network host="smtp.gmail.com" defaultCredentials="false"
port="587" userName ="yourmail#gmail.com" password="yourpassword" />
</smtp>
</mailSettings>
</system.net>
And in your page_load
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
var user = HttpContext.Current.User;
if (user != null && user.Identity.IsAuthenticated)
{
var id = Membership.GetUser().ProviderUserKey;
SendMail(id.ToString());
}
}
}
public void SendMail(string id)
{
var message = new MailMessage();
message.IsBodyHtml = true;
message.From = new MailAddress("yourmail");
message.To.Add(new MailAddress("tomail"));
message.Subject= "user access page";
message.Body = "Your Message";// add id here
var client = new SmtpClient();
client.Send(message);
}
if you using vb try something like this.
This way, you will not need to change your web.config
Dim mailobject As New System.Net.Mail.MailMessage()
Dim myCred As New System.Net.NetworkCredential("yourmail#gmail.com", "password")
mailobject.To.Add("tomail")
mailobject.Subject = "User Access The Page"
mailobject.From = New System.Net.Mail.MailAddress("frommail")
mailobject.IsBodyHtml = True
mailobject.Body = "Your Message"
Dim SmtpMail As New System.Net.Mail.SmtpClient("smtp.gmail.com")
SmtpMail.UseDefaultCredentials = False
SmtpMail.EnableSsl = True
SmtpMail.Credentials = myCred
SmtpMail.Port = 587
SmtpMail.Send(mailobject)

Fair question. But you really do not want to do this unless you have a very limited amount of visitors. See the answer above on how to do it.
Better write everything into a table and send yourself a digest at certain interval per hour/day. This way you can also do some reporting afterwards using that table.

Related

Change from while sending mail using WepAPi

I send a mail using WebApi. Mail sent successfully. I want to change from mail in it and I change it using below code but it take from mail as 'testweb#gmail.com'. I use testweb#gmail.com in webconfig and I want to set from as testfrom#gmail.com.
but not working as per below code and when I got mail it always from 'testweb#gmail.com' instead of 'testfrom#gmail.com'
Note : I use above emails only to ask question while development I use my real mail id.
Is there any other way to achieve this? Or I need to change anything.
Below is my code to send mail:
public static bool SendMail(string toAddress, string subject, string body)
{
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("testfrom#gmail.com");
msg.To.Add(new MailAddress(toAddress));
msg.Subject = subject;
msg.Body = body;
msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.EnableSsl = true;
smtp.Send(msg);
return true;
}
catch (Exception ex)
{
return false;
}
}
Below is webconfig smpt setting:
<mailSettings>
<smtp from="testweb#gmail.com">
<network host="smtp.gmail.com" port="587" userName="testweb#gmail.com" password="test" />
</smtp>
</mailSettings>
also try with below setting in webconfig:
<smtp deliveryMethod="Network">
<network host="smtp.gmail.com" port="587" userName="testweb#gmail.com" password="test" />
</smtp>
</mailSettings>
You cant!!
You are using the google SMTP server, it doesn't allows to change the from address, if you want to change the from address try using some other smtp provider that allows so.

Activation email in vb.net identity

I hope someone can shed some light for me, I have an application that users can register and have un-commented lines in the register.aspx.vb file to allow for email.
Protected Sub CreateUser_Click(sender As Object, e As EventArgs)
Dim userName As String = Email.Text
Dim manager = Context.GetOwinContext().GetUserManager(Of ApplicationUserManager)()
Dim signInManager = Context.GetOwinContext().Get(Of ApplicationSignInManager)()
Dim user = New ApplicationUser() With {.UserName = userName, .Email = userName}
Dim result = manager.Create(user, Password.Text)
If result.Succeeded Then
' For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
Dim code = manager.GenerateEmailConfirmationToken(user.Id)
Dim callbackUrl = IdentityHelper.GetUserConfirmationRedirectUrl(code, user.Id, Request)
manager.SendEmail(user.Id, "Confirm your account", "Please confirm your account by clicking here.")
signInManager.SignIn(user, isPersistent := False, rememberBrowser := False)
IdentityHelper.RedirectToReturnUrl(Request.QueryString("ReturnUrl"), Response)
Else
ErrorMessage.Text = result.Errors.FirstOrDefault()
End If
End Sub
The problem is though, that I need to add email settings similar to that at http://www.asp.net/identity/overview/features-api/account-confirmation-and-password-recovery-with-aspnet-identity#email but not sure how to call this, and does this need to go in the Identity.config under EmailService??? Do I need to add something to the web.config????
Add the details to your web.config:
<system.net>
<mailSettings>
<smtp from=“-your-sending-email-address-">
<network defaultCredentials="false" host=“-mail.example.com-" password=“—password-" userName=“—email—account-" />
</smtp>
</mailSettings>
</system.net>
More information:https://msdn.microsoft.com/en-us/library/w355a94k(v=vs.110).aspx

Email not working from Godaddy Server

First of all, I hope to be at the right place, I apologize in advance if this is not the case, please re-orient myself.
So here I have a worry, I have a website www.artipik.com with two forms, online quote and contact form.
My website is hosted by godaddy and servers were updated summers there for 15 days and my form are no longer work, which put me in a very problematic position. I'm a graphic designer not a coder, hence my difficulty changed the code.
I had the godaddy technical service and he gave it to me I think part of the solution. I found the place to change the code, but I can not seem to fit, I'm testing, I no further error messages but I do not receive any email I send.
My current ASP code is as follows:
<%
'-----version CDONTS-----
Set Mail = Server.CreateObject("CDONTS.NewMail")
Mail.BodyFormat = 1 '0:html/1:plain text
Mail.MailFormat = 1 '0:MIME/1:plain text
Mail.From = envoyeur
Mail.To = receveur
if copie<>vbnullstring then Mail.Bcc = copie
if sujet=vbnullstring then sujet="Formulaire de contact Artipik.com"
Mail.Subject = sujet
Mail.Body = message
Mail.Send
set Mail=Nothing
'response.cookies("email")=envoyeur
%>
and the new code that I have to adapt is the following (Using CDOSYS to Send Email from Your Windows Hosting Account):
// language -- C#
// import namespace
using System.Web.Mail;
private void SendEmail()
{
const string SERVER = "relay-hosting.secureserver.net";
MailMessage oMail = new System.Web.Mail.MailMessage();
oMail.From = "emailaddress#domainname";
oMail.To = "emailaddress#domainname";
oMail.Subject = "Test email subject";
oMail.BodyFormat = MailFormat.Html; // enumeration
oMail.Priority = MailPriority.High; // enumeration
oMail.Body = "Sent at: " + DateTime.Now;
SmtpMail.SmtpServer = SERVER;
SmtpMail.Send(oMail);
oMail = null; // free up resources
}
Thank you for your help.
Julian
System.Web.Mail.MailMessage is obsolete. You should use System.Net.Mail.MailMessage instead. It works almost exactly the same (warning! untested):
private void SendEmail()
{
using (var message = new MailMessage())
{
message.From = new MailAddress("emailaddress#domainname");
var toAddress = new MailAddress("emailaddress#domainname");
message.To.Add(toAddress);
message.Subject = "Test email subject";
message.Body = "Sent at: " + DateTime.Now;
message.Priority = MailPriority.High;
message.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Send(message);
}
}
Add this to your web.config:
<configuration>
<system.net>
<mailSettings>
<smtp from="emailaddress#domainname">
<network host="your-smtp-host-address" />
</smtp>
</mailSettings>
</system.net>
</configuration>
Recently I've had the same problem, could solve it throug the next way:
in web.config:
<system.net>
<mailSettings>
<smtp from="contacto#mydomain.com">
<network host="relay-hosting.secureserver.net" password="MyPass" port="25" enableSsl="false" defaultCredentials="false" userName="contacto#mydomain.com"/>
</smtp>
</mailSettings>
</system.net>
in codebehind:
MailMessage m = new MailMessage();
m.IsBodyHtml = true;
m.Body = "My Message";
m.To.Add("jhon.doe#yahoo.com");
m.Subject = "Go Daddy Emails Go Live!";
SmtpClient smtp = new SmtpClient();
smtp.Send(m);

Unable to send in asp.net application

I want to send an e-mail, but it shows the following error:
try
{
string filename = Server.MapPath("~/App_Data/FeedBack.txt");
string mailbody = System.IO.File.ReadAllText(filename);
mailbody = mailbody.Replace("##Name##", txtName.Text);
mailbody = mailbody.Replace("##Email##",txtEmail.Text);
mailbody = mailbody.Replace("##contact##",txtContact.Text);
mailbody = mailbody.Replace("##Commnect##",txtSuggestion.Text);
MailMessage myMessage = new MailMessage();
myMessage.Subject = "Responce from website";
myMessage.Body = mailbody;
myMessage.From = new MailAddress("amitverma0511#gmail.com","Amit Kumar");
myMessage.To.Add(new MailAddress("amitverma1150#gmail.com", "Amit Verma"));
SmtpClient mysmptclient = new SmtpClient("localhost");
mysmptclient.Host = "smtp.gmail.com";
mysmptclient.Port = 587;
mysmptclient.Credentials = new System.Net.NetworkCredential("amitverma0511#gmail.com", "Myemailpassword");
mysmptclient.Send(myMessage);
formatTable.Visible = false;
lblMail.Text = "Your Feedback has been sent successfully";
lblMail.ForeColor = Color.Green;
}
catch(Exception expt)
{
lblMail.Text = "unable to sent the feedback .Check your smpt server"+expt.Message;
lblMail.ForeColor = Color.Red;
}
My web.config file looks like this:
<system.net>
<mailSettings>
<smtp from="amitverma0511#gmail.com">
<network host="smtp.gmail.com" port='587' password="myemailpass" userName="amitverma0511#gmail.com"/>
</smtp>
</mailSettings>
</system.net>
As far as I know, you are overwriting your web.config settings by using localhost (your IIS) as a SMTP Server.
If you have not configured your IIS to serve as a SMTP server this naturally will not work.
But, if you want to use localhost and not the google servers just use this 3 lines:
SmtpClient MySmtpClient = new SmtpClient();
MySmtpClient.UseDefaultCredentials = true;
MySmtpClient.Send(mailMessage);
You should remove the string localhost from SmtpClient mysmptclient = new SmtpClient("localhost"); and it should work, if all your other settings are correct.
Further information can be found here.

how to send a confirmation email in asp.net, cannot get IIS pickup directory error

I want to send a confirmation email to user in registration page. The following code is the related part:
try
{
SmtpClient sc = new SmtpClient();
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
string Ema = u.UserMail.ToString();
MailAddress gonderen = new MailAddress("admin#gmail.com", "Hello");
sc.Host = "smtp.gmail.com";
sc.Port = 587;
mail.To.Add(Ema);
mail.Subject = "Confirmation Message";
mail.From = gonderen;
mail.IsBodyHtml = true;
mail.BodyEncoding = System.Text.Encoding.GetEncoding("ISO-8859-9");
mail.Body = "<html><body>";
sc.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
sc.Send(mail);
MESSAGE(true, "Sending mail is successful");
}
catch (Exception)
{
MESSAGE(true, "Sending mail is unsuccessful!");
}
But it does not send an email to related user. I have looked at forums and I added to web.config the following part:
<system.net>
<mailSettings>
<smtp from="myaddress#gmail.com ">
<network host="smtp.gmail.com" defaultCredentials="false"
port="587" userName ="myaddress#gmail.com" password="password" />
</smtp>
</mailSettings>
</system.net>
But anything didn't change. Then i have debugged and it enters into the try statement and when it comes to sc.Send(mail);, it drops to catch. Where is my mistake?
Additionally, during debug i realized that it shows this error: cannot get IIS pickup directory. I have controlled whether I have a smtp service or not from services but i couldn' t see this service. Is this error related to that service?
Thanks in advance.
Why not use GMAIL as the SMTP server, since you are defining a GMAIL account, by defining the host as "smtp.gmail.com", and port at 587?
Specific details here: http://support.google.com/mail/bin/answer.py?hl=en&answer=13287
Thanks for your helps.
I have solved this problem. I have changed my code as the following one:
try
{
SmtpClient sc = new SmtpClient();
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
string Ema = u.UserMail.ToString();
MailAddress gonderen = new MailAddress("admin#gmail.com", "Hello");
sc.Host = "smtp.gmail.com";
sc.Port = 587;
sc.EnableSsl = true;
mail.To.Add(Ema);
mail.Subject = "Confirmation Message";
mail.From = gonderen;
mail.IsBodyHtml = true;
mail.BodyEncoding = System.Text.Encoding.GetEncoding("ISO-8859-9");
mail.Body = "<html><body>";
mail.Body += "</body></html>";
sc.DeliveryMethod = SmtpDeliveryMethod.Network;
sc.Send(mail);
MESAJ(true, "Sending mail is successful");
}
catch (Exception)
{
MESAJ(true, "Sending mail is unsuccessful!");
}
and then i have set my web.config like the following :
<mailSettings>
<smtp deliveryMethod="Network"
from="admin#gmail.com">
<network defaultCredentials="false" host="smtp.gmail.com" port="587"
userName="admin#gmail.com" password="password" />
</smtp>
</mailSettings>
that works.. :)

Resources