Solved

show the mail before sending

Posted on 2006-07-23
19
273 Views
Last Modified: 2012-05-05
In ASP.NET 2.0 in C#
I prepare a mail to be send with all that is needed
but I don't want to send it automatically.
I want to show the mail on the screen, to let the user check it and then
the user will press the button of send.
This is my preparing of the mail. What do I have to change?
   
           MailAddress to = new MailAddress("anat@business-solutions.co.il");
            MailAddress from = new MailAddress("anat@business-solutions.co.il");
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, to);

            message.Subject = "test email1";
            message.Body = "test email1 from business-solutions on 18/06/2006";
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
             smtp.Send(message);
Thankyou
Anat
0
Comment
Question by:ANAT2403
  • 11
  • 5
  • 3
19 Comments
 
LVL 23

Expert Comment

by:apresto
ID: 17162812
Hi ANAT2403,

What you could do is create the message object with page scope and do this in the page_load event:

MailAddress to = new MailAddress("anat@business-solutions.co.<wbr/>il");
            MailAddress from = new MailAddress("anat@business-solutions.co.<wbr/>il");
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, to);

            message.Subject = "test email1";
            message.Body = "test email1 from business-solutions on 18/06/2006";
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
            Response.Write( message.Body );

then have a button on the page, that when clicked that sends it with:

smtp.Send(message);

Apresto
0
 
LVL 23

Expert Comment

by:apresto
ID: 17162813
What code do you have at the moment for the page
0
 

Author Comment

by:ANAT2403
ID: 17162873
Hi Apresto
This situation might happen while I have various codes in a page. I guess I should open a new page for this
because the response.write will overwrite the content of my page
Why did you say to put it in the page_load event?
Thanks
Anat

0
 
LVL 23

Expert Comment

by:apresto
ID: 17162904
I just said page_load as an example, i meant put it somewhere where it would load when the page did, as i assumed that the information to be used in the message obvject would be submitted to this page from another.

You dont need to use response.write - you could populate a literal control or some other web control with the message body

Apresto
0
 

Author Comment

by:ANAT2403
ID: 17162927
But all this will not appear as mail.
I want it to appear as mail in outlook
Anat
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17162953
Do you want to integrate with Outlook, or have a web page that resembles the Outlook mail item form?

Bob
0
 

Author Comment

by:ANAT2403
ID: 17163050
I would like to integrate with Outlook.
Anat
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17163054
That is a messy proposition, since Outlook is COM, and by its very nature a single threaded environment, while ASP.NET is multi-threaded.  You need to be very certain that you can't accomplish this another way before going down the Outlook road.

Bob
0
 

Author Comment

by:ANAT2403
ID: 17163062
Any how how I do it through Outlook?
Anat
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 17163080
1) You need Outlook installed on a server, if you are publishing from a machine to a remote machine

2) You need to add a COM reference for the Microsoft Outlook type library to the project

3) Use something like this:

  Public Shared Sub SendMail(ByVal [to] As String, ByVal subject As String, ByVal body As String, ByVal attachment As String)

    Dim app As New Outlook.Application
    Dim mail As Outlook.MailItem = app.CreateItem(Outlook.OlItemType.olMailItem)
    mail.To = [to]
    mail.Subject = subject
    mail.BodyFormat = Outlook.OlBodyFormat.olFormatRichText
    mail.Body = body
    mail.Display()

    app.Quit()

    System.Runtime.InteropServices.Marshal.ReleaseComObject(app)
    System.Runtime.InteropServices.Marshal.ReleaseComObject(mail)

  End Sub

4) Depending on the version of Outlook, and the COM wrapper that gets generated, you might have to fix that example code.

5) If you use Outlook, then you don't need all that SMTP stuff

Bob
0
 

Author Comment

by:ANAT2403
ID: 17163093
The thing is that in some mails I have to send it automatically with the SMTP and in some mails through the outlook.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17163103
Ok, I am not trying to dictate any course of action.  I assumed that you were replacing SMTP with Outlook.  If you need both, then keep it.

Bob
0
 

Author Comment

by:ANAT2403
ID: 17183517
Hi,
I want to use the code you sent me for opening the outlook but I need it in C# and I don't succeed
to tranform it to C#. I guess I need some using namespaces in the beginning.
Can you help?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 17183648
Try this:

public static void SendMail(string toAddress, string subject, string body, string attachment)
{
    Outlook.Application app = new Outlook.Application()
    Outlook.MailItem mail = app.CreateItem(Outlook.OlItemType.olMailItem);
    mail.To = toAddress;
    mail.Subject = subject;
    mail.BodyFormat = Outlook.OlBodyFormat.olFormatRichText;
    mail.Body = body;
    mail.Display();

    app.Quit();

    System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(mail);

}

Bob
0
 

Author Comment

by:ANAT2403
ID: 17184119
I can't write the Outlook.Application. It is not recognized. I need some namespace. You know which one?
Anat
0
 

Author Comment

by:ANAT2403
ID: 17184225
Hi,
OK I undesrtood. I have to add Reference to COM Microsoft Outlook 11.0 Object Library.
I will continue now and inform you.
Anat
0
 

Author Comment

by:ANAT2403
ID: 17184988
mail.Display() force me to give a parameter.  (oject Modal)
What parameter can I give?

 
0
 

Author Comment

by:ANAT2403
ID: 17185219
Hi,
It sends the mail but it does not open the outlook . I want to open the outlook see the prepared mail, approve it
and then send it by pressing the send button of the outlook.
I would like to main the command
<a href="mailto:anat@business-solutions.co.il?subject=Your subject&to=anat@business-solutions.co.il&body=Enter your message here">Mail</a>
in the code behind so that I can give values to the body ,to and subject.
How do I do it?
0
 

Author Comment

by:ANAT2403
ID: 17185301
Ok I ask the question and answer them.
In the display i had to write display(true)
OK it works OK.
Thankyou
Anat
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now