 |
|
แปลง code ส่งอีเมล์ ให้กลายเป็น class ส่งอีเมล์ ทำอย่างไรครับ |
|
 |
|
|
 |
 |
|
ผมได้เขียน code สำหรับส่งอีเมล์ ผ่าน LotusNotes Domino (จริงๆ ไป copy code มาอีกที)
ปกติที่เครื่องทดสอบ ผมจะใช้ไฟล์ myuser.id ของผมเองในการใช้ส่งอีเมล์
ทีนี้เวลาจะใช้งานจริง ต้องเปลี่ยนไฟล์ myuser.id เป็นของ user ที่ใช้งานระบบ ไม่งั้นเมล์ที่ส่งออกไป จะกลายเป็นว่าผมเป็นคนส่ง
ผมได้ลอง search จากเว็บ พบว่ามี method ที่ใช้เปลี่ยนไฟล์ user.id ได้ ก็นึกว่ารอดแล้ว
แต่สุดท้ายไม่รอดครับ พอ run แล้วขึ้น error ดังนี้
Code
Retrieving the COM class factory for component with CLSID {29131537-2EED-1069-BF5D-00DD011186B7} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
ใช้เวลาหาว่าจะมีวิธีแก้หรือไม่ มีอยู่ 2 กระทู้ แนะนำวิธีแก้ให้ครับ วิธีแรกบอกให้เรียกใช้ใน block using(){}
และแนะนำให้เขียนดังนี้
Code (C#)
using(NotesSession ns = new NotesSession())
{
ns.Initialize("notespassword");
// ...
}
แต่ก็ไปต่อไม่ถูก ติด error
Code
'Domino.NotesSession': type used in a using statement must be implicitly convertible to 'System.IDisposable'
ส่วนอีกวิธีหนึ่ง บอกว่าให้เขียนเป็น class ขึ้นมาแล้วเรียกใช้แยกต่างหาก และประกันว่าวิธีนี้ใช้ได้ผล แต่ไม่มีตัวอย่างให้ดู
http://
http://stackoverflow.com/questions/5508868/
ผมเห็นว่า วิธีนี้มีความเป็นไปได้ แต่ติดตรงที่ตั้งแต่เขียนโปรแกรมมา ไม่เคยเขียน class เลย
ท่านใด พอจะแนะนำวิธีแปลง code นี้ให้เป็น class ได้บ้างครับ
Code (C#)
protected void SendButton_Click(object sender, DirectEventArgs e)
{
{ // send email to related person
string mailDoc = this.txtContent.Text.ToString();
string mailTo = this.txtTo.Text.ToString();
string mailCc = this.txtCc.Text.ToString();
string mailFrom = this.txtFrom.Text.ToString();
string mailSubj = this.txtSubject.Text.ToString();
// Switch Notes User.id file
NotesRegistration reg = new NotesRegistration();
reg.RegistrationServer = this.txtNotesServer.Text.ToString();
reg.SwitchToID("C:\Lotus\Notes\Data\enduser.id", "notespassword");
//Create new notes session
NotesSession ns = new NotesSession();
NotesDatabase db = null;
NotesDocument doc = null;
NotesMIMEEntity body = null;
NotesStream stream = null;
object oItemValue = null;
//Initialize Notes Session
ns.Initialize("notespassword");
ns.ConvertMime = false;
//Get Database via server name & c:\notes\data\mailfilename.nsf if not found set to false to not create one
db = ns.GetDatabase(this.txtNotesServer.Text.ToString(), this.txtNotesFile.Text.ToString(), false);
//If the database is not already open then open it.
if (!db.IsOpen) { db.Open(); }
//////////// code send mail //////////
stream = ns.CreateStream();
//Create the notes document
doc = db.CreateDocument();
//Set document type
doc.ReplaceItemValue("Form", "Memo");
//sent notes memo fields (To: CC: Bcc: Subject etc)
doc.ReplaceItemValue("SendTo", mailTo);
if (!string.IsNullOrEmpty(mailCc)){
doc.ReplaceItemValue("CC:", mailCc);
}
doc.ReplaceItemValue("Subject", mailSubj);
//body
body = doc.CreateMIMEEntity("Body");
stream.WriteText(mailDoc, EOL_TYPE.EOL_CRLF);
body.SetContentFromText(stream, "text/html;charset=UTF-8", MIME_ENCODING.ENC_NONE);
body.EncodeContent(MIME_ENCODING.ENC_NONE);
//send email & pass in byRef field, this case SendTo (always have this, cc or bcc may not always be there.
oItemValue = doc.GetItemValue("SendTo");
doc.Send(false, ref oItemValue);
//release mail resources.
mailDoc = string.Empty;
body = null;
doc = null;
stream = null;
ns.ConvertMime = true;
db = null;
ns = null;
reg = null;
}
}
ขอขอบคุณเป็นอย่างสูงมา ณ ที่นี้ครับ
Tag : .NET, Web (ASP.NET), C#
|
|
 |
 |
 |
 |
Date :
2014-02-06 15:11:40 |
By :
Aod47 |
View :
1505 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
|